24 Hour Time/Date Formatting Loophole

WildBurrow

New Member
Joined
Apr 5, 2021
Messages
41
Office Version
  1. 365
Platform
  1. 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:
  • 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
I thought I had code that would catch any issues (below), but as stated above, it doesn't catch an 'a' or 'p' inserted next to the time.

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?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top