Code to highlight rows when data range is before previous date.

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,751
Office Version
  1. 365
Platform
  1. Windows
Tricky one to explain this. I have a file where I need to know if any row of dates are earlier than the rows before. The code first needs to look at column K and all the numbers that match. In the example below I have highlighted in yellow the ones that are incorrect. If you look at N6&7/O6&7 the date is earlier than P5/Q5 above. The same with N12&13/O12&13 is earlier than P11/Q11.

The ones in blue are as it should be, they all follow on from each other. Like I say difficult to explain but please let me know if more clarification is needed.

Excel 2010
KLMNOPQ
1IdentifierSMSYEMEY
2M10306960000001031994121997
3M10306960000001031994121997
4M10306960000001031994121997
5M10306960000002031994121995
6M10306960000002031994121999
7M10306960000002031994121999
8M18912550000000062016122016
9M18912550000000062016122016
10M18912550000000062016122016
11M18900900000001022015122015
12M18900900000001022015
13M18900900000001022015
14M18900900000002022015
15M18900900000002022015
16M18900900000002022015
17M18912170000009042015042017
18M18912170000009042016042017
19M18912170000002022015122015
20M18912170000002012016122016
21M18912170000002012017
22M18912170000007052016
23M18912170000007052016
24M18912170000007052016

<colgroup><col style="width: 25pxpx" https:="" www.mrexcel.com="" forum="" usertag.php?do="list&action=hash&hash=DAE7F5"" target="_blank"></colgroup><colgroup><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>

Edit

In hindsight its probably better if the code looks for matches in column C rather than K please.
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If P6560 was truly blank then that line of code wouldn't get executed:

Code:
    If Cells(thisRow, "P").Value <> "" Then

This is a more defensive option for dealing with non-numeric cells:

Code:
Public Sub CheckSequentialDates()

Dim lastRow As Long
Dim thisRow As Long
Dim firstRow As Long
Dim inError As Boolean

' Get the last row of data
lastRow = Cells(Rows.Count, "C").End(xlUp).Row + 1

' Set up other variables
inError = False
firstRow = 1

' Process all rows
For thisRow = 2 To lastRow
    ' Is this the same identifier as the last row?
    If Cells(thisRow, "C").Value = Cells(thisRow - 1, "C").Value Then
        ' If the dates are the same as the last row then there's nothing to do
        If Cells(thisRow, "N").Value <> Cells(thisRow - 1, "N").Value _
        Or Cells(thisRow, "O").Value <> Cells(thisRow - 1, "O").Value _
        Or Cells(thisRow, "P").Value <> Cells(thisRow - 1, "P").Value _
        Or Cells(thisRow, "Q").Value <> Cells(thisRow - 1, "Q").Value Then
            ' Make sure we have numeric values
            If IsNumeric(Cells(thisRow, "O").Value) _
            And IsNumeric(Cells(thisRow, "N").Value) _
            And IsNumeric(Cells(thisRow - 1, "Q").Value) _
            And IsNumeric(Cells(thisRow - 1, "P").Value) Then
                ' Is the start date before the previous end date?
                If DateSerial(Cells(thisRow, "O").Value, Cells(thisRow, "N").Value, 1) < DateSerial(Cells(thisRow - 1, "Q").Value, Cells(thisRow - 1, "P").Value, 1) Then
                    ' Flag this group as being in error
                    inError = True
                End If
            End If
        End If
    Else
        ' Highlight the group if there's been an error
        If inError Then Range(Cells(firstRow, "C"), Cells(thisRow - 1, "Q")).Interior.Color = vbYellow
        
        ' The new group is not in error - remember the row it started on
        inError = False
        firstRow = thisRow
    End If
    
    ' Check for invalid dates on this row
    If IsNumeric(Cells(thisRow, "N").Value) _
    And IsNumeric(Cells(thisRow, "O").Value) _
    And IsNumeric(Cells(thisRow, "P").Value) _
    And IsNumeric(Cells(thisRow, "Q").Value) Then
        If DateSerial(Cells(thisRow, "O").Value, Cells(thisRow, "N").Value, 1) > DateSerial(Cells(thisRow, "Q").Value, Cells(thisRow, "P").Value, 1) Then
            inError = True
        End If
    End If
Next thisRow

End Sub

WBD
 
Upvote 0
Unfortunately now that highlights all rows without an end date in P & Q? In my very first post rows 14, 15 & 16 are being highlighted when they should be left clear as there is nothing wrong with them.
 
Upvote 0
OK. *Sigh*. It's hard sailing blind without having the actual data to hand. One last try:

Code:
Public Sub CheckSequentialDates()

Dim lastRow As Long
Dim thisRow As Long
Dim firstRow As Long
Dim inError As Boolean

' Get the last row of data
lastRow = Cells(Rows.Count, "C").End(xlUp).Row + 1

' Set up other variables
inError = False
firstRow = 1

' Process all rows
For thisRow = 2 To lastRow
    ' Is this the same identifier as the last row?
    If Cells(thisRow, "C").Value = Cells(thisRow - 1, "C").Value Then
        ' If the dates are the same as the last row then there's nothing to do
        If Cells(thisRow, "N").Value <> Cells(thisRow - 1, "N").Value _
        Or Cells(thisRow, "O").Value <> Cells(thisRow - 1, "O").Value _
        Or Cells(thisRow, "P").Value <> Cells(thisRow - 1, "P").Value _
        Or Cells(thisRow, "Q").Value <> Cells(thisRow - 1, "Q").Value Then
            ' Make sure we have numeric values
            If Trim(Cells(thisRow, "O").Value) <> "" _
            And Trim(Cells(thisRow, "N").Value) <> "" _
            And Trim(Cells(thisRow - 1, "Q").Value) <> "" _
            And Trim(Cells(thisRow - 1, "P").Value) <> "" Then
                ' Is the start date before the previous end date?
                If DateSerial(Cells(thisRow, "O").Value, Cells(thisRow, "N").Value, 1) < DateSerial(Cells(thisRow - 1, "Q").Value, Cells(thisRow - 1, "P").Value, 1) Then
                    ' Flag this group as being in error
                    inError = True
                End If
            End If
        End If
    Else
        ' Highlight the group if there's been an error
        If inError Then Range(Cells(firstRow, "C"), Cells(thisRow - 1, "Q")).Interior.Color = vbYellow
        
        ' The new group is not in error - remember the row it started on
        inError = False
        firstRow = thisRow
    End If
    
    ' Check for invalid dates on this row
    If Trim(Cells(thisRow, "N").Value) <> "" _
    And Trim(Cells(thisRow, "O").Value) <> "" _
    And Trim(Cells(thisRow, "P").Value) <> "" _
    And Trim(Cells(thisRow, "Q").Value) <> "" Then
        If DateSerial(Cells(thisRow, "O").Value, Cells(thisRow, "N").Value, 1) > DateSerial(Cells(thisRow, "Q").Value, Cells(thisRow, "P").Value, 1) Then
            inError = True
        End If
    End If
Next thisRow

End Sub

WBD
 
Upvote 0
Success! Its only highlighted what I expected it to, I will try on more problems and let you know. Thank you so much for your time WBD.
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,713
Members
449,464
Latest member
againofsoul

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