Validate date format is correct
Hi,
I am a beginer to VBA,
so this is my question :
When i input a date in xls cell (which has a formatting set to date Eg : 14-Mar-99) , i need to check if date is in the range 1 - 31, month should be in the range 1-12 , and year should be in the range 1906-2106.
i used the follwing condition :
variable dt is the date i am getting from the xls cell.
For Year:
If CInt(Year(dt)) < 1906 Or CInt(Year(dt)) > 2106 Then
display error
For month:
If CInt(Month(dt)) < 1 Or CInt(Month(dt)) > 12 Then
display error
for day:
If CInt(Day(dt)) < 1 Or CInt(Day(dt)) > 31 Then
display error
When i enter valid date , this works fine.
But, when i enter a invalid value in xls cell eg :32/12/378373 , i get a type Run time Error : 13
Type Mismatch.
Is this the right way to check? And am i getting Type mismatch because the cell's format is of type date?
Should i just catch the Type mismatch error for "invalid date"?
Thanks,
Gayatri
|