Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Remove Duplicates in a Column

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To remove duplicates in a column with Excel VBA, use the RemoveDuplicates method on a Range that covers the column, passing the column number inside that range. For example, Range("A1:A100").RemoveDuplicates Columns:=1, Header:=xlYes deletes duplicate rows in place and leaves the first occurrence. That single line is the whole job; the rest of this guide covers making it safe, looping through multiple columns, and fixing the cases where it silently does nothing.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer; on Mac, Excel > Preferences > Ribbon & Toolbar).
  • A workbook saved as .xlsm (macro-enabled) if you want the code to persist, or you can paste code into a one-off module and run it without saving.
  • A backup copy of your data. RemoveDuplicates deletes rows permanently and cannot be undone with Ctrl+Z after the macro finishes.
  • Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac). Menu labels can differ slightly by Excel version and region.

Step-by-Step

  1. Back up the sheet. Right-click the sheet tab and choose Move or Copy > Create a copy, or save a separate copy of the file. This is the only reliable undo.
  2. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook, choose Insert > Module.
  3. Write the basic macro. Paste this into the module:
    Sub RemoveDupesInColumnA()
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        ws.Range("A1:A100").RemoveDuplicates Columns:=1, Header:=xlYes
    End Sub
    Replace "Sheet1" with your sheet name and adjust the range to cover your data.
  4. Understand the arguments. Columns:=1 means "use the first column of the range as the key." If your range is A1:C100 and you want duplicates judged on column A only, still use Columns:=1. To judge on A and C together, use Columns:=Array(1, 3).
  5. Set Header correctly. Use Header:=xlYes if row 1 contains a heading so Excel does not treat it as data. Use xlNo if there is no heading. Getting this wrong either deletes your heading or keeps a duplicate of it.
  6. Make the range dynamic. Hard-coded ranges break when data grows. Use the last used row:
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    ws.Range("A1:A" & lastRow).RemoveDuplicates Columns:=1, Header:=xlYes
  7. Run the macro. Press F5 in the editor, or close the editor and use Developer > Macros, select the macro, and click Run.
  8. Check the result. Excel keeps the first occurrence of each value and deletes the rest of the row. If you need a record of what was removed, copy the range to another sheet before running the macro.
  9. Apply to several columns at once. To deduplicate a whole table based on every column, use ws.Range("A1:C" & lastRow).RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes. This removes rows where all three values match another row.
  10. Save as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A normal .xlsx save strips the macro.

Common Mistakes

  • Forgetting Header. The default is xlNo, so a heading in row 1 gets treated as data and can be deleted or duplicated.
  • Wrong Columns number. The number refers to position within the range, not the worksheet column letter. In Range("C1:C100"), column C is Columns:=1.
  • Static ranges. A range like A1:A50 stops working the moment data reaches row 51.
  • Running on the wrong sheet. ActiveSheet changes with whatever tab is open. Name the worksheet explicitly.
  • Expecting Undo. Ctrl+Z does not restore rows deleted by VBA. Only a backup or a pre-copy does.
  • Case and whitespace. RemoveDuplicates ignores case ("Apple" and "apple" count as duplicates) but does not trim spaces, so "Apple " and "Apple" are kept as different values.

Tips

  • Store the original row count before and after to report how many rows were removed: before = ws.UsedRange.Rows.Count, run the method, then compare.
  • To preserve the original data, copy the range to a helper sheet, run RemoveDuplicates there, and leave the source untouched.
  • If you only want to flag duplicates rather than delete them, use COUNTIF in a helper column or conditional formatting instead.
  • Turn off Application.ScreenUpdating around the call when processing large ranges to speed it up, and turn it back on afterwards.
  • Keep one macro per task and name it clearly, so the Developer > Macros list stays readable.

Troubleshooting

Nothing happens. Check the sheet name and range actually contain data. A range that points at an empty sheet runs without error and without effect.

Run-time error 1004. Usually a misspelled sheet name, a closed workbook, or a range reference that does not exist. Verify the name in the Project pane and that the workbook is open.

The header disappeared. Header was set to xlNo. Restore from backup and rerun with xlYes.

Duplicates remain. Likely trailing spaces or invisible characters. Clean the column first with TRIM or Power Query, then rerun. Also confirm you are keying on the column you think you are.

Macro missing after reopening. The file was saved as .xlsx. Save as .xlsm and rerun.

Rows shifted unexpectedly. RemoveDuplicates deletes entire rows within the range, so data in adjacent columns outside the range is not affected, but data inside those rows is. Widen the range only when you intend to remove whole records.

Frequently Asked Questions

Does RemoveDuplicates work on a single column only?

It works on any range. Columns:=1 keys on one column; Columns:=Array(1, 2) keys on two or more, removing a row only when all keyed values match.

Is the first or last duplicate kept?

The first occurrence in the range is kept; later duplicates are deleted.

Can I undo a VBA RemoveDuplicates?

No. Ctrl+Z does not reverse it. Keep a backup or copy the data to another sheet first.

Does it treat "Apple" and "apple" as duplicates?

Yes, the comparison is case-insensitive. Leading or trailing spaces are not ignored, so trim them first.

Can I remove duplicates without deleting rows?

Yes, use a helper column with COUNTIF to flag duplicates and filter or delete manually, or use Power Query's Remove Duplicates step on a copy.

Why does my macro delete the header row?

Header was set to xlNo or omitted. Set Header:=xlYes when row 1 contains a heading.