Find and Delete Duplicates But Keep The First One

transparencia

New Member
Joined
May 16, 2011
Messages
4
I want to delete duplicates rows but keep the first one of each duplicate set.

Example:
(The Red Rows are Removed)

Row 1___1000____________20
Row 2___1000 ___________ 20
Row 3___1000 ___________ 20


How can I this with a VBA function?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello & Welcome to the Board,

Do your columns have a header row? I'm guessing it would just be two columns.

If so change Header:=xlNo to xlYes

Code:
Sub test()
    Dim LR As Long: LR = Range("A" & Rows.Count).End(xlUp).Row
    ActiveSheet.Range("A1:B" & LR).RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
 
Upvote 0
Try

Code:
Sub test2()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    With Range("A" & i)
        If .Value = .Offset(-1).Value And .Offset(, 1).Value = .Offset(-1, 1).Value Then .EntireRow.Delete
    End With
Next i
End Sub
 
Upvote 0
Thanks guys but I'm not sure how to apply those functions because I forgot to add that I added a helper column that concatenates everything, like this:

Col A___Col B__________Col C______Col D
Row 1___1000____________20_________100020
Row 2___1000 ___________ 20__________100020
Row 3___1000 ___________ 20__________100020


How can I apply the above functions to COL D? And can I just have the rows selected so I can delete them manually?
 
Upvote 0
Try

Code:
Sub test2()
Dim LR As Long, i As Long, r As Range
LR = Range("D" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    With Range("D" & i)
        If .Value = .Offset(-1).Value Then
            If r Is Nothing Then
                Set r = Range("D" & i)
            Else
                Set r = Union(r, Range("D" & i))
            End If
        End If
    End With
Next i
If Not r Is Nothing Then r.EntireRow.Select
End Sub
 
Upvote 0
It worked when I tested it. The sheet must be the active sheet when you run it, and sorted as in your example. Maybe

Code:
Sub test2()
Dim LR As Long, i As Long, r As Range
LR = Range("D" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    With Range("D" & i)
        If .Value = .Offset(-1).Value Then
            If r Is Nothing Then
                Set r = Range("D" & i)
            Else
                Set r = Union(r, Range("D" & i))
            End If
        End If
    End With
Next i
If r Is Nothing Then
    MsgBox "No duplicates found"
Else
    r.EntireRow.Select
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,509
Messages
6,179,192
Members
452,893
Latest member
denay

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