+4 votes
67 views
in Office by (242k points)
reopened
Excel: Delete duplicate rows

1 Answer

+5 votes
by (1.6m points)
 
Best answer

Delete duplicate lines in Excel using a macro

You can find out how to delete duplicate rows in Excel in our guide..

image image

Sometimes double entries occur in Excel. If the duplicates are duplicate values, here are tips on how to remove them. However, if you have duplicate entire lines in your document, you need to create a macro in Excel.

Delete duplicate lines in Excel using a macro

A macro is required to delete duplicate rows in Excel. To do this, press [Alt] + [F11] to open the Macro Editor. Then click on the " Insert " tab and then on " Module ".

image
This is what the view looks like in the macro editor.

Write the following lines in the macro you just created. You can copy and paste directly into the macro editor:

Sub DoppelteZeilenEntfernen ()

Dim last line As Long
Dim line As Long

last line = Range ("A" & Rows.Count) .End (xlUp) .Row
For line = last line To 1 Step -1
If WorksheetFunction.CountIf (Range ("A1: A "& line), Range (" A "& line))> 1 Then
Rows (line) .EntireRow.Delete

End If
Next

End Sub
Sub DoppelteZeilenEntfernen()

Dim LetzteZeile As Long
Dim Zeile As Long

LetzteZeile = Range("A" & Rows.Count).End(xlUp).Row
For Zeile = LetzteZeile To 1 Step -1
If WorksheetFunction.CountIf(Range("A1:A" & Zeile), Range("A" & Zeile)) > 1 Then
Rows(Zeile).EntireRow.Delete

End If
Next

End Sub

Finally, run the macro you just created. Now all lines with duplicate entries are deleted, so that only the first line remains.

Note: The code shown here only applies to duplicates in column A. If you want to examine any other column for duplicate entries, you must replace "A1" or "A" throughout the code with the appropriate column name..


...