Macro To Highlight Invalid Dates

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,783
Office Version
  1. 365
Platform
  1. Windows
I have files with a column with dates in like the format below. Some are invalid where the end date is before the start date (highlighted)! I need a macro please so I can put in my Personal Macro Workbook for use on different files, that will highlight these errors when the code is run. In this case we can use column A but I can amend code for the column to suit. Thanks.

06/2016->12/2019
06/2016->12/2019
02/2015->04/2017
02/2015->04/2017
02/2015->04/2017
02/2018->04/2017
02/2015->04/2017
02/2015->04/2017
02/2019->04/2017
02/2015->04/2017
02/2015->
02/2015->
05/2016->
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
assuming the info is in one column (A) in the format you have shown above and each entry in a new cell perhaps something like

VBA Code:
Dim cel As range

For Each cel In range("a1:A" & range("A" & Rows.count).End(xlUp).Row)
    If DateValue(Mid(cel, 1, 7)) < DateValue(Mid(cel, 10, 7)) Then
    Else
        cel.Font.Color = vbRed
    End If
Next cel
 
Upvote 0
assuming the info is in one column (A) in the format you have shown above and each entry in a new cell perhaps something like

VBA Code:
Dim cel As range

For Each cel In range("a1:A" & range("A" & Rows.count).End(xlUp).Row)
    If DateValue(Mid(cel, 1, 7)) < DateValue(Mid(cel, 10, 7)) Then
    Else
        cel.Font.Color = vbRed
    End If
Next cel
Thanks but I got a runtime error '13' type mismatch on this line?

If DateValue(Mid(cel, 1, 7)) > DateValue(Mid(cel, 10, 7)) Then
 
Upvote 0
Thanks but I got a runtime error '13' type mismatch on this line?

If DateValue(Mid(cel, 1, 7)) > DateValue(Mid(cel, 10, 7)) Then
is your cel set out exactly like this "06/2016->12/2019"
 
Upvote 0
Or 06/2016-> etc... with no end date.

what are you expecting it to do if no end date
also did you "DIM cel as range" and have you used cel elsewhere in your code
 
Upvote 0
Or 06/2016-> etc... with no end date.
the type mismatch will be because of the missing second date. if you amend the code to below it works fine
VBA Code:
Dim cel As Range

For Each cel In Range("a1:A" & Range("A" & Rows.Count).End(xlUp).Row)

    If Mid(cel, 10, 7) <> "" Then
    
         If DateValue(Mid(cel, 1, 7)) < DateValue(Mid(cel, 10, 7)) Then
           Else
              cel.Font.Color = vbRed
         End If
    
    End If

Next cel

End Sub
 
Upvote 0
Or 06/2016-> etc... with no end date.

what are you expecting it to do if no end date
also did you "DIM cel as range" and have you used cel elsewhere in your code
I am still getting the error on If DateValue(Mid(cel, 1, 7)) < DateValue(Mid(cel, 10, 7)) Then

"what are you expecting it to do if no end date" do nothing as it will not be an invalid date.

"also did you "DIM cel as range" and have you used cel elsewhere in your code" I have just run the code as you supplied.
 
Upvote 0
I am still getting the error on If DateValue(Mid(cel, 1, 7)) < DateValue(Mid(cel, 10, 7)) Then

"what are you expecting it to do if no end date" do nothing as it will not be an invalid date.

"also did you "DIM cel as range" and have you used cel elsewhere in your code" I have just run the code as you supplied.
Can you attach a proper sample sheet
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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