WildBurrow
New Member
- Joined
- Apr 5, 2021
- Messages
- 41
- Office Version
- 365
- Platform
- Windows
I'm using a custom cell format in two cells (mm/dd/yy hh:mm). These cells are used to calculate elapsed time. Users are instructed to input 24-hour time (e.g. 1:00 PM is 13:00); however, they are managing to bypass my attempts to prevent the document from being saved with an invalid date format.
What I've discovered is this:
Is there any way to catch this issue in the Worksheet_Change(ByVal Target As Range) section?
What I've discovered is this:
- If I put in 11/3/22 13:00, which is the desired format - things calculate properly
- If I put in 11/3/22 1:00 PM, the information converts to 11/3/22 13:00, and things calculate properly
- If I put in 11/3/22 1:00p (notice no space after the time), it won't calculate properly
VBA Code:
'Error message if discovery date not valid
If Not Application.Intersect(Target, Range("I3")) Is Nothing Then
If (Not IsDate(Target.Value)) Then
Application.EnableEvents = False
Target.Value = "00/00/00 00:00"
Target.Activate
Application.EnableEvents = True
MsgBox "Please enter valid date and time.", vbOKOnly + vbExclamation, "Date and Time Format: mm/dd/yy hh:mm"
End If
End If
'Error message if reported date not valid
If Not Application.Intersect(Target, Range("I4")) Is Nothing Then
If (Not IsDate(Target.Value)) Then
Application.EnableEvents = False
Target.Value = "00/00/00 00:00"
Target.Activate
Application.EnableEvents = True
MsgBox "Please enter valid date and time.", vbOKOnly + vbExclamation, "Date and Time Format: mm/dd/yy hh:mm"
End If
End If
Is there any way to catch this issue in the Worksheet_Change(ByVal Target As Range) section?