I am trying to come up with a lean and error-proofed macro to delete rows containing duplicate values in a column A. I have two solutions and both have their advantages. None of them are exactly what I want.
I need rows containing duplicates deleted but leaving the last row that contained the duplicate.
This one is awesome. It has no loop and works instantaneously. The problem is that it deletes subsequent rows containing duplicates hence leaving the first occurrence of the duplicate (And I need the last/ or second - most show up only twice)
Sub Delete() ActiveSheet.Range("A:E").RemoveDuplicates Columns:=1, Header:=xlNo End SubThis one goes from the bottom and deletes duplicates. It lasts longer than the first one ( I have around 6k rows) But the issue with this one is that it doesnt delete them all. Some duplicates are left and they are deleted after I run the same code again. Even smaller number of duppes is still left. Basically need to run it up to 5 times and then I end up with clean list.
`
Sub DeleteDup()
Dim LastRowcheck As Long, n1 As Long, rowschecktodelete As Long LastRowcheck = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row For n1 = 1 To LastRowcheck With Worksheets("Sheet1").Cells(n1, 1) If Cells(n1, 1) = Cells(n1 + 1, 1) Then Worksheets("Sheet1").Cells(n1, 1).Select Selection.EntireRow.Delete End If End With Next n1 End Sub
` Is there a way to improve any of these to work well or is there a better solution? Any info is greatly appreciated. Thanks