VBA - Delete Duplicate Rows in columns but Keep Last raw

jomakram

New Member
Joined
May 19, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I need help please, i want to delete raws if cells in columns A and B are duplicated, but i want to keep the last duplicated raw and delete all previous ones.

i got this code, it works but it keeps the fist raw not the last.

Sub DelDuplicates()
'
' Macro working but keeps 1st record and delete the rest
'

Dim current As String
ActiveSheet.Range("A1").Activate
Do While ActiveCell.Value <> ""
current = ActiveCell.Address
ActiveCell.Offset(1, 0).Activate
Do While ActiveCell.Value <> ""
If ((ActiveSheet.Range(current).Value = ActiveCell.Value) And (ActiveSheet.Range(current).Offset(0, 1).Value = ActiveCell.Offset(0, 1).Value)) Then
' next raw
Selection.Offset(1, 0).Select
' end of next raw
ActiveSheet.Rows(ActiveCell.Row).Delete
Else
ActiveCell.Offset(-1, 0).Activate
End If
Loop
ActiveSheet.Range(current).Offset(1, 0).Activate
Loop
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
1589947187882.png

This is an example, if cells in column A are duplicated and also in B are duplicated, then delete the first raws (is this case leave the last one that has the date Jun 08, 2019)
 
Upvote 0
Try this on a copy of your data.
VBA Code:
Sub jomakram()
Dim a As Long, b As Long
With ActiveSheet
    b = Cells(Rows.Count, 1).End(xlUp).Row
    For a = b To 2 Step -1
           If Cells(a, 1).Value = Cells(a - 1, 1).Value Then
                If Cells(a, 2).Value = Cells(a - 1, 2).Value Then
                    If Cells(a, 1).Value <> "" Then Rows(a - 1).Delete
                End If
           End If
    Next a
End With
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

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