VBA macros · Data
VBA macro to remove duplicates based on one column in Excel
Remove duplicate rows based on the values in one column with RemoveDuplicates in VBA, and report how many rows were removed.
Customer lists, email lists and imports often contain the same record twice. Excel's Data › Remove Duplicates does the job, but the dialog has to be set up every time. This macro removes rows whose key column (for example an email address or an order number) repeats an earlier row, keeps the first occurrence, and tells you how many rows went.
Option Explicit
Sub RemoveDuplicatesInColumn()
Const KEY_COL As Long = 1 ' 1 = column A of the data
Dim ws As Worksheet
Dim data As Range
Dim lastRow As Long, lastCol As Long
Dim before As Long, after As Long
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, KEY_COL).End(xlUp).Row
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
If lastRow < 3 Then
MsgBox "Not enough data to check.", vbInformation
Exit Sub
End If
' The whole table, headings in row 1
Set data = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol))
before = lastRow - 1
On Error GoTo Fail
data.RemoveDuplicates Columns:=KEY_COL, Header:=xlYes
after = ws.Cells(ws.Rows.Count, KEY_COL).End(xlUp).Row - 1
MsgBox before - after & " duplicate row(s) removed. " & after & " row(s) left.", vbInformation
Exit Sub
Fail:
MsgBox "Could not remove duplicates: " & Err.Description, vbExclamation
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickRemoveDuplicatesInColumnand click Run. - Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
How it works
The macro builds the data range from A1 to the last row of the key column and the last heading in row 1, using End(xlUp) and End(xlToLeft) as explained in Find the last row and build dynamic ranges in VBA. It assumes the table starts in A1 and has headings.
Range.RemoveDuplicates is the VBA version of the ribbon command. Columns:=KEY_COL says which column(s) of the range decide whether a row is a duplicate; the number counts from the first column of the range, not of the sheet. Header:=xlYes protects the heading row. The first occurrence of each value is kept and later ones are removed, with the rows below moving up.
Counting the rows before and after gives the message at the end. The error handler catches problems such as a protected sheet or merged cells inside the data, which make RemoveDuplicates fail.
Change it
To treat a row as a duplicate only when several columns match, for example first name and last name, pass an array of column numbers:
data.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYesIf you build the array in a variable first, wrap the variable in brackets: Columns:=(cols). Without the brackets Excel rejects a Variant array with a confusing error, a long-standing quirk.
To compare all columns, pass every number, for example Array(1, 2, 3, 4, 5) for five columns. To remove duplicates from a single column list without touching the columns next to it, use only that column as the range: ws.Range("A1:A" & lastRow).RemoveDuplicates Columns:=1, Header:=xlYes. To see the duplicates before deleting anything, run VBA macro to highlight duplicate values in Excel first. In Microsoft 365 the UNIQUE function returns a de-duplicated copy without changing the source.
What can go wrong
Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data. RemoveDuplicates keeps the first occurrence, so sort the data first if you want to keep, say, the newest record (newest at the top).The comparison ignores upper and lower case, but not extra spaces: [email protected] and *[email protected] * with a trailing space are different values. Run VBA macro to trim spaces in selected cells in Excel on the key column first.
If the data is an Excel table, use the table's range instead: ws.ListObjects(1).Range.RemoveDuplicates Columns:=1, Header:=xlYes.
RemoveDuplicates exists in Excel 2007 and later on Windows and in Excel 2016 and later on Mac.Related: VBA macro to highlight duplicate values in Excel, VBA macro to delete rows based on a cell value in Excel, UNIQUE.