Isdate function is to check whether variables is valid date or not and the return is Boolean either True or False.
Syntax : IsDate(expression), Return : True/False
1) Below example for True in Result.
VBA Vode:
Sub Examples_IsDateTrue()
Dim MyDate() As Date
ReDim Preserve MyDate(1): MyDate(1) = "01/07/2022"
ReDim Preserve MyDate(2): MyDate(2) = "01/99" '= 1000000 True?
ReDim Preserve MyDate(3): MyDate(3) = "01/13/2022"
ReDim Preserve MyDate(4): MyDate(4) = "13/01/2022"
ReDim Preserve MyDate(5): MyDate(5) = "12-01-2022"
ReDim Preserve MyDate(6): MyDate(6) = CDate(42565) '14/7/2016
Debug.Print IsDate(MyDate(1)) '= True
Debug.Print IsDate(MyDate(2)) '= True
Debug.Print IsDate(MyDate(3)) '= True
Debug.Print IsDate(MyDate(4)) '= True
Debug.Print IsDate(MyDate(5)) '= True
Debug.Print IsDate(MyDate(6)) '= True
End Sub
Note: With variables original declare as date may different result compare with direct test. For example direct test IsDate(1) False but declare as date MyDate(1) = 1, IsDate(MyDate(1)) become True. Even with number 1 to 1,000,000 still true but more than that become error "Run-time error '6': Overflow".
2) Below example for False in Result.
VBA Vode:
Sub Examples_IsDateFalse()
Debug.Print IsDate(42565) '= False
Debug.Print IsDate("8") '= False
Debug.Print IsDate(15) '= False
Debug.Print IsDate("24") '= False
Debug.Print IsDate("String") '= False
End Sub
Note: Without Dim declaration the result will be different.
Microsoft Reference-Isdate-functionOther Reference-Isdate-function.htm
Practice makes perfect. Thank You.
excel training beginners
coding in vba
excel training online
visual basic for applications
No comments:
Post a Comment