Try this:
usage: ValidateDate ActiveCell.Text
'DD/MM/YYYY" format
Function ValidateDate(ByVal pvDateCode) As Boolean
Dim D As Integer
Dim LeapYear As Boolean
Dim M As Integer
Dim Result As Boolean
Dim Y As Integer, i As Integer
Dim sDte As String, sPart As String
'If Not IsNumeric(pvDateCode) Then Exit Function
sDte = Format(pvDateCode, "dd/mm/yyyy")
i = InStr(pvDateCode, "/")
If i = 0 Then
MsgBox "Invalid date"
Else
D = Left(sDte, i - 1)
sPart = Mid(sDte, i + 1)
i = InStr(sPart, "/")
M = Left(sPart, i - 1)
i = InStrRev(sDte, "/")
Y = Mid(sDte, i + 1)
End If
If M > 12 Then Result = False: GoTo endit
If Y < 1900 Then Result = False: GoTo endit
LeapYear = (Y Mod 100 <> 0 And Y Mod 4 = 0) Or (Y Mod 400 = 0)
Select Case M
Case 1, 3, 5, 7, 8, 10, 12
If D >= 1 And D <= 31 Then Result = True
Case 2
If LeapYear Then
If D >= 1 And D <= 29 Then Result = True
Else
If D >= 1 And D <= 28 Then Result = True
End If
Case 4, 6, 9, 11
If D >= 1 And D <= 30 Then Result = True
End Select
endit:
If Not Result Then MsgBox "Format: dd/mm/yyyy", , "Invalid Date"
ValidateDate = Result
End Function
Bookmarks