Private Sub Worksheet_Change(ByVal Target As Range)
Dim chkRng As Range
Dim rng As Range
Dim cell As Range
Dim cellAdd As String
Dim errMsg As String
Dim m1, m2, y1, y2
' ***ENTER RANGE YOU WANT TO APPLY THIS CODE TO HERE***
Set chkRng = [COLOR=#ff0000]Range("B1:B100")[/COLOR]
' See if cell updated falls within range
Set rng = Intersect(Target, chkRng)
' Exit if no cells updated in that range
If rng Is Nothing Then Exit Sub
' Loop through updated cells in indicated range
For Each cell In rng
cellAdd = cell.Address(0, 0)
' See if entry matches requried length
If (Len(cell) <> 17) Or (Mid(cell, 8, 3) <> " - ") Or (Mid(cell, 3, 1) <> "/") Or (Mid(cell, 13, 1) <> "/") Then
errMsg = "Entry in cell " & cellAdd & " is not in format 'MM/YYYY - MM/YYYY'"
Exit For
End If
' Get date pieces
m1 = Left(cell, 2)
m2 = Mid(cell, 11, 2)
y1 = Mid(cell, 4, 4)
y2 = Mid(cell, 14, 4)
' Check to see if months are valid
If IsNumeric(m1) And (m1 > 0) And (m1 <= 12) Then
Else
errMsg = "Month in first date in cell " & cellAdd & " is not valid or not in format 'MM/YYYY - MM/YYYY'"
Exit For
End If
If IsNumeric(m2) And (m2 > 0) And (m2 <= 12) Then
Else
errMsg = "Month in second date in cell " & cellAdd & " is not valid or not in format 'MM/YYYY - MM/YYYY'"
Exit For
End If
' Check to see if months are valid
If IsNumeric(y1) And (y1 >= 1900) And (y1 <= 2100) Then
Else
errMsg = "Year in first date in cell " & cellAdd & " is not valid or not in format 'MM/YYYY - MM/YYYY'"
Exit For
End If
If IsNumeric(y2) And (y2 >= 1900) And (y2 <= 2100) Then
Else
errMsg = "Year in second date in cell " & cellAdd & " is not valid or not in format 'MM/YYYY - MM/YYYY'"
Exit For
End If
Next cell
' Return any errors
If errMsg <> "" Then
MsgBox errMsg, vbOKOnly, "ENTRY ERROR!"
Application.EnableEvents = False
cell.ClearContents
Application.EnableEvents = True
End If
End Sub