Also, I need to validate it only 2 numbers for Date and 2 numbers for Month. and 4 numbers for Year. (DD/MM/YYYY)
What's wrong with the current code?
You are really confusing us, it only allows dd/mm/yyyy entry as you requested.
If you are not able to adjust the code, you should ask everything in the first place.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, r As Range
Set rng = Intersect(Target, Range("M:M,T:U,Z:Z,AB:AD,AH:AI,CS:ST"), Rows("3:" & Rows.Count))
If rng Is Nothing Then Exit Sub
Application.EnableEvents = False
With CreateObject("VBScript.RegExp")
.Pattern = "^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\d{4}$"
For Each r In rng
If Len(Target.Text) Then
If (.test(r.Text) * IsDate(r.Text)) = 0 Then
MsgBox "Invalid date", , r.Text & " " & r.Address(0, 0)
r.ClearContents
End If
End If
Next
End With
Application.EnableEvents = True
End Sub
Bookmarks