Check Cells for Real Dates

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Whats the easiest way to check if a cell is a valid date before doing something else

Need to Check A7, C7 L7 and Z7 if any are either text or incorrect date entries exit sub with a message box

Thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
It depends on your opinion of a 'valid date'

One thing that comes to mind
VBA Code:
Select Case False
    Case IsDate(Range("A7").Value), _
         IsDate(Range("C7").Value), _
         IsDate(Range("L7").Value), _
         IsDate(Range("Z7").Value)
        MsgBox "error"
        Exit Sub
End Select
But in my opinion, it is not a perfect solution.
 
Upvote 0
It depends on your opinion of a 'valid date'

One thing that comes to mind
VBA Code:
Select Case False
    Case IsDate(Range("A7").Value), _
         IsDate(Range("C7").Value), _
         IsDate(Range("L7").Value), _
         IsDate(Range("Z7").Value)
        MsgBox "error"
        Exit Sub
End Select
But in my opinion, it is not a perfect solution.
It depends on your opinion of a 'valid date'

the cells are already formatted as a date so if they enter 15/12/21 or 15-12-21 or 15/12 the cell would be 15 Dec 21 which is fine
but if they enter 15.12.21 it enters as 15.12.21 or 15 enters as 15 Jan 00 which I don't want. Also it cant be text.

PS if I just wanted to check one cell, i.e. A7, instead of Select Case WOuld it just be
VBA Code:
if IsDate(Range("A7").value = False
then Msgbox "error"
Exit Sub
 
Upvote 0
or 15 enters as 15 Jan 00 which I don't want
Apart from that one, it will work as needed.

That would be evaluated as 15/01/1900 which is technically a valid date.

If the dates are being entered by the user before the code runs then applying data validation to those cells with a specified start and end date will be much easier. Then you can simply exit the code if any of the cells are empty (assuming that they must all be completed).
 
Upvote 0
Thanks

Didn't want to use data validation as entries can cover a few thousand rows. I'll try the code tomorrow
 
Upvote 0
With the code, you will need something like this to avoid the 15 Jan 00 type of entry. The reference date can be adjusted as needed if you need valid dates to go back earlier than 2015.
VBA Code:
Dim mDate As Date
mDate = #1/1/2015#
If Not IsDate(Range("A7").Value) Or Range("A7").Value <= mDate Then
        MsgBox "error"
        Exit Sub
End If
 
Upvote 0
Thanks Jason,
Using the above how do I combine A7, C7, L7 and Z7
Also forgot to mention that the user can leave one of the Cells Blank, so the <=mDate is good if they enter a really old date, but it also picks up blank as really old date
 
Upvote 0
Also forgot to mention that the user can leave one of the Cells Blank
This will not check if more than one is empty, but it should be ok for the rest.

VBA Code:
Dim mDate As Date, c As Range
mDate = #1/1/2015#
For Each c In Range("A7,C7,L7,Z7")
    If Not IsEmpty(c) Then
        If Not IsDate(c.Value) Or c.Value <= mDate Then
            MsgBox "error"
            Exit Sub
        End If
    End If
Next
Noting that you mentioned that it would be over 1000 or so rows, how do you intend to determine which row it should be looking at?
Intersect is one way that comes to mind, but how you identify the row to check and what you want to do next will dictate the best method.
 
Upvote 0
Thanks, that seems to work

I'm using, Worksheet_SelectionChange(ByVal Target As Range) then add the row number to a Cell

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("D2")) Is Nothing Then
  Range("Z1").Value = Target.Row

although not sure how to combine the target row and Column i.e. to get Range("A7,C7,L7,Z7")
 
Last edited:
Upvote 0
Something like this maybe? Where does D2 come into the process?
VBA Code:
Application.EnableEvents = False
Dim mDate As Date
mDate = #1/1/2015#
If Not Intersect(Target,Range("A:A,C:C,L:L,Z:Z")) Is Nothing Then
    If Not IsEmpty(c) Then
        If Not IsDate(Target.Value) Or Target.Value <= mDate Then
            Range("Z1").Value = Target.Row
        End If
    End If
Next
Application.EnableEvents = True
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,509
Members
449,089
Latest member
RandomExceller01

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