If columns D and E match, delete a row with oldest date(in column B) VBA

Lenna

Active Member
Joined
Jun 25, 2014
Messages
269
Hello,

I need to compare values in columns D and E with a row beneath. If they match, I would like keep a row with the most recent date(column B) and delete the other. All data are sorted and duplicates are removed.

Here is how far I got with the code... . Please suggest the rest.

Code:
Sub MostRecentDate()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lr + 1
    If
        Range("D" & r).Value = Range("D" & r + 1).Value And _
        Range("E" & r).Value = Range("E" & r + 1).Value Then
        'some code
       
    End If
    Next r
End Sub
Thanks,

Lenna
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this on a copy of your file. Note that I changed the sequence of your For...Next statement so that it will start from the bottom and work upward on the sheet. This eliminates the skipping of rows caused by the shifting upon deletion or a row or rows.
Code:
Sub MostRecentDate()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 2 Step -1
        If Range("D" & r).Value = Range("D" & r - 1).Value And _
        Range("E" & r).Value = Range("E" & r - 1).Value Then
        'some code
            If Range("B" & r).Value > Range("B" & r -1).Value Then
                Rows(r - 1).Delete xlUp
            Else
                Rows(r).Delete xlUp
            End If
       
        End If
    Next r
End Sub
 
Upvote 0
Thank you, it works great!

Try this on a copy of your file. Note that I changed the sequence of your For...Next statement so that it will start from the bottom and work upward on the sheet. This eliminates the skipping of rows caused by the shifting upon deletion or a row or rows.
Code:
Sub MostRecentDate()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 2 Step -1
        If Range("D" & r).Value = Range("D" & r - 1).Value And _
        Range("E" & r).Value = Range("E" & r - 1).Value Then
        'some code
            If Range("B" & r).Value > Range("B" & r -1).Value Then
                Rows(r - 1).Delete xlUp
            Else
                Rows(r).Delete xlUp
            End If
       
        End If
    Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,188
Members
448,554
Latest member
Gleisner2

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