Deleting duplicates differently

Craig1

Active Member
Joined
Jun 11, 2009
Messages
322
Hi All,
I have the code below that works perfectly to delete rows that are duplicated.
Is it possible to change the code so it deletes from the top down instead of the usual bottom up. This is because sometimes the duplicates that are lower down the imported list are updated and do have more info than the original.
So deleting downwards would be the best option but I'm not sure if this can be done and how?

Private Sub CommandButton22_Click()
LR = Cells(Rows.Count, "B").End(xlUp).Row
For i = LR To 2 Step -1
If WorksheetFunction.CountIf(Range("B:B"), Cells(i, "B").Value) > 1 Then
Rows(i).EntireRow.Delete
End If
Next i

End Sub

Thanks in advance

Craig.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Presumably you're aware for the reason to delete from the bottom up, yes? If you go from the top down, you have to adjust your row pointer whenever a row is deleted so it doesn't skip over lines which move up as the result of the row above them being deleted.

Try this:-
Code:
For i = 2 To LR
  If WorksheetFunction.CountIf(Range("B:B"), Cells(i, "B").Value) > 1 Then
    Rows(i).EntireRow.Delete
    i = i - 1
  End If
Next i
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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