PDA

View Full Version : Delete duplicate rows based on first 2 columns?



clhare
02-21-2008, 06:38 AM
I need to delete duplicate rows in a table using just the first 2 columns for comparison. I found lots of things on the Internet, but nothing that works for me. Can anyone help?

Bob Phillips
02-21-2008, 09:32 AM
When you say the first 2 columns Cheryl, do you mean testing those columns for values, or checking those columns against previous, row 1 or something?

MikeO
02-21-2008, 10:31 AM
I think I understand the question...
Assuming the data is already sorted on the first two columns so that the duplicate rows are together, this is one of many possible ways to accomplish what I think you're asking.

Sub DeleteDuplicateRows()
Dim CheckCell As Range
Dim DeleteTheRow As Boolean

Set CheckCell = ActiveSheet.Range("A65536").End(xlUp)

Do While CheckCell.Row > 1

DeleteTheRow = False

If CheckCell = CheckCell.Offset(-1) And CheckCell.Offset(, 1) = CheckCell.Offset(-1, 1) Then
DeleteTheRow = True
End If

Set CheckCell = CheckCell.Offset(-1)

If DeleteTheRow = True Then
CheckCell.Offset(1).EntireRow.Delete
End If

Loop
End Sub

clhare
02-21-2008, 11:54 AM
Yea! Thank you Mike! That macro does exactly what I need!

:cloud9:

MikeO
02-21-2008, 12:02 PM
You're very welcome...glad I could help.