remove duplicate lines, but not delete rows

excelos

Well-known Member
Joined
Sep 25, 2011
Messages
592
Office Version
  1. 365
Platform
  1. Windows
hello

I would like to ask you if you know a way to delete duplicate lines and leave the first instance of them, but not deleting rows as well, as shown in this screenshot:
111825233419.png

thanks!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the board Excelos. Try this on a copy of your worksheet:
Code:
Sub RemoveSomeDupInfo()
Dim rng As Range, delRng As Range, dataRws As Long
Dim dStr As String, v1(1 To 3), v2(1 To 3), ctr As Long

Set rng = ActiveSheet.Range("a1").CurrentRegion
dataRws = rng.Rows.Count - 1
Set rng = rng.Offset(1, 0).Resize(dataRws)
Set delrws = Nothing
For Each rw In rng.Rows
    For i = 1 To 3
        v1(i) = rw.Cells(1, i)
        v2(i) = rw.Offset(1, 0).Cells(1, i)
        If v1(i) <> v2(i) Then
            Exit For
        Else
            ctr = ctr + 1
        End If
        If ctr = 3 Then
            If delrws Is Nothing Then
                Set delrws = rw.Offset(1, 0).Resize(, 3)
            Else
                Set delrws = Union(delrws, rw.Offset(1, 0).Resize(, 3))
            End If
        End If
    Next i
ctr = 0
Next rw
If delrws Is Nothing Then
    MsgBox "No duplicates found"
Else
    delrws.ClearContents
End If
End Sub
 
Upvote 0
Welcome to the board Excelos. Try this on a copy of your worksheet:
Code:
Sub RemoveSomeDupInfo()
Dim rng As Range, delRng As Range, dataRws As Long
Dim dStr As String, v1(1 To 3), v2(1 To 3), ctr As Long

Set rng = ActiveSheet.Range("a1").CurrentRegion
dataRws = rng.Rows.Count - 1
Set rng = rng.Offset(1, 0).Resize(dataRws)
Set delrws = Nothing
For Each rw In rng.Rows
    For i = 1 To 3
        v1(i) = rw.Cells(1, i)
        v2(i) = rw.Offset(1, 0).Cells(1, i)
        If v1(i) <> v2(i) Then
            Exit For
        Else
            ctr = ctr + 1
        End If
        If ctr = 3 Then
            If delrws Is Nothing Then
                Set delrws = rw.Offset(1, 0).Resize(, 3)
            Else
                Set delrws = Union(delrws, rw.Offset(1, 0).Resize(, 3))
            End If
        End If
    Next i
ctr = 0
Next rw
If delrws Is Nothing Then
    MsgBox "No duplicates found"
Else
    delrws.ClearContents
End If
End Sub

it works brilliantly, thanks!

however, the above xls I posted is just an example, will I have any problem with other xls files? I mean that the duplicates are in columns b and c, etc

I was thinking that I should determine which columns to scan for duplicates
 
Upvote 0
I assumed that the data are sorted so that duplicates are already grouped together and that you are using a tabular format with column headers like the example you posted. Also that the duplication of interest is in the first three columns of the table. Finally, that the worksheet you want to apply the code to is the active worksheet when you run the code.

If your table layout changes, say for example the table starts in cell D3 instead of A1, then you need to change "a1" in the first set statement to "D3". If you want to look for duplicates in columns other than the first three columns of the table, then you would have to make some changes to the code to ensure that those columns are being checked for duplicate entries.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,719
Members
452,939
Latest member
WCrawford

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