Date Validation Error

WildBurrow

New Member
Joined
Apr 5, 2021
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I have the following code to ensure valid date;

VBA Code:
'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 = Empty
            target.Activate
            Application.EnableEvents = True
            MsgBox "Please enter valid date and time.", vbOKOnly + vbExclamation, "Discovery Date and Time Format:   mm/dd/yy hh:mm"
        End If
    End If

The cell has a custom format for date as mm/dd/yy hh:mm

I've just discovered that is someone adds a zero to the beginning of the month, it isn't triggering the message. For example:
Actual date/time: 04/02/24 17:49
Entered date/time 004/02/24 17:49

Any idea why the IsDate function isn't recognizing this as an error?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
When someone actually types "004/02/24 17:49" into a cell, it is not recognized as a date/time, and it is stored as text. Therefore IsDate will return FALSE.

BTW that will also screw up any calculations on the worksheet using that date.
 
Upvote 0
Try adding string pattern "##/##/## ##:##" as a criteria:
VBA Code:
    If Not Application.Intersect(Target, Range("I3")) Is Nothing Then
        If (IsDate(Target.Value)) And Target.Text Like "##/##/## ##:##" Then 'string pattern corresponding to "mm/dd/yy hh:mm"
            'do nothing
        Else
            Application.EnableEvents = False
            Target.Value = Empty
            Target.Activate
            Application.EnableEvents = True
            MsgBox "Please enter valid date and time.", vbOKOnly + vbExclamation, "Discovery Date and Time Format:   mm/dd/yy hh:mm"
        End If
    End If
 
Upvote 0
Try.
Instead of

VBA Code:
  If (Not IsDate(target.Value))  Then
Use
VBA Code:
  If (Not IsDate(target.Value)) And Not WorksheetFunction.IsText(target.Value) Then
 
Upvote 0
When someone actually types "004/02/24 17:49" into a cell, it is not recognized as a date/time, and it is stored as text. Therefore IsDate will return FALSE.

BTW that will also screw up any calculations on the worksheet using that date.
6StringJazzer:
"BTW that will also screw up any calculations on the worksheet using that date." .....Yep, it did just that.
 
Upvote 0
Try adding string pattern "##/##/## ##:##" as a criteria:
VBA Code:
    If Not Application.Intersect(Target, Range("I3")) Is Nothing Then
        If (IsDate(Target.Value)) And Target.Text Like "##/##/## ##:##" Then 'string pattern corresponding to "mm/dd/yy hh:mm"
            'do nothing
        Else
            Application.EnableEvents = False
            Target.Value = Empty
            Target.Activate
            Application.EnableEvents = True
            MsgBox "Please enter valid date and time.", vbOKOnly + vbExclamation, "Discovery Date and Time Format:   mm/dd/yy hh:mm"
        End If
    End If
Akuini:

This did the trick. Thank you for your quick reply.
 
Upvote 0
Try.
Instead of

VBA Code:
  If (Not IsDate(target.Value))  Then
Use
VBA Code:
  If (Not IsDate(target.Value)) And Not WorksheetFunction.IsText(target.Value) Then
kvsrinivasamurthy,
I tried your code but found that it did nothing. It failed to trigger the message box. This is how I've typed it:
VBA Code:
'Discovery date not valid
    If Not Application.Intersect(target, range("I3")) Is Nothing Then
        If (Not IsDate(target.Value)) And Not WorksheetFunction.IsText(target.Value) Then
            Application.EnableEvents = False
            target.Value = Empty
            target.Activate
            Application.EnableEvents = True
            MsgBox "Please enter valid date and time.", vbOKOnly + vbExclamation, "Discovery Date and Time Format:   mm/dd/yy hh:mm"
        End If
    End If
 
Upvote 0
TRy
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Application.Intersect(Target, Range("I3")) Is Nothing Then
   Dim K As Double
   On Error Resume Next
   K = Target.Value
        If K = 0 Then
            Application.EnableEvents = False
            Target.Value = Empty
            Target.Activate
            Application.EnableEvents = True
            MsgBox "Please enter valid date and time.", vbOKOnly + vbExclamation, "Discovery Date and Time Format:   mm/dd/yy hh:mm"
        End If
    End If

End Sub
 
Upvote 0
Solution
TRy
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Application.Intersect(Target, Range("I3")) Is Nothing Then
   Dim K As Double
   On Error Resume Next
   K = Target.Value
        If K = 0 Then
            Application.EnableEvents = False
            Target.Value = Empty
            Target.Activate
            Application.EnableEvents = True
            MsgBox "Please enter valid date and time.", vbOKOnly + vbExclamation, "Discovery Date and Time Format:   mm/dd/yy hh:mm"
        End If
    End If

End Sub
That worked. Thank you.

There is a slight performance difference between your code and the code offered by Akuini (suggested identifying the string pattern).
If a user put in "AM" or "PM" in the cell, Akuini's code corrected the entry to the 24-hour time formatting.
Your code picked up on the "AM" or "PM" as text and triggered the error message.

However, both did the trick.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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