In Excel: paste Sub RemoveDuplicatesVBA() into the VBA editor (Alt + F11 → Insert ▸ Module), press F5 to run, and save as .xlsm to keep the macro.
VBA macro: Remove Duplicates with VBA
Sub RemoveDuplicatesVBA()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
ws.Range("A1:A" & lastRow).RemoveDuplicates Columns:=1, Header:=xlYes
End SubRemoveDuplicates checks column A (Columns:=1) and assumes the first row is a header (Header:=xlYes). Extend the range and add more column numbers to deduplicate across multiple columns.
How to run this macro
- Press Alt + F11 to open the VBA editor.
- Insert > Module.
- Paste the code above.
- Press
F5, or close the editor and run it from Developer > Macros. - Save the file as .xlsm so the macro is kept.
Open Excel and navigate to Developer ▸ Visual Basic (or press Alt + F11).
Select Insert ▸ Module from the menu bar inside the VBA editor.
Copy and paste Sub RemoveDuplicatesVBA() into the Module.
Click anywhere inside the Sub body and press F5 to run.
Close the VBA editor and save the file as .xlsm.
What this does
The Remove Duplicates with VBA macro lets you remove duplicate rows using a VBA macro in Excel without repeating the steps by hand. Paste the Sub RemoveDuplicatesVBA() code into the VBA editor (Alt + F11 → Insert ▸ Module) and run it from Developer ▸ Macros or by pressing F5 inside the editor. Once the macro runs, Excel executes every step programmatically — what might take several manual passes through your data completes in a single click. Because the code is stored in the workbook, you can reuse the Remove Duplicates with VBA macro any time the same task arises without rebuilding the steps from scratch. Most people learn this as a sequence of clicks and forget it by next week; learning it as a pattern instead is what lets you apply it to the next, slightly different version of the problem without starting from scratch. That is the difference this page is trying to make. Treat “excel vba remove duplicates” as a small repeatable workflow rather than a one-off click you hope to remember next time. Use a small test block before the live file, so any surprise in the affected duplicates shows up while it is still harmless. When this is a ribbon command, the selection matters more than the button: confirm the range, apply the command, then spot-check the output before saving. That turns a data step that keeps your analysis trustworthy into a method you can reuse, explain, and defend when the workbook leaves your screen.
A worked example
Practical use: you run the same remove duplicate rows using a VBA macro task every month on a new export. Instead of repeating the steps manually, paste Sub RemoveDuplicatesVBA() once into a Personal Macro Workbook (or into the workbook itself), and run it each month via Developer ▸ Macros ▸ Run. The Remove Duplicates with VBA macro applies the same logic to every fresh dataset — consistent, repeatable, and immune to copy-paste slips. Use Sub RemoveDuplicatesVBA() every time you need to remove duplicate rows using a VBA macro reliably at scale. The macro eliminates the most common sources of error in manual work: missed rows, wrong column references, and inconsistent criteria. Once the code is tested and trusted, you can run it repeatedly with one click — zero cognitive overhead, zero variation in output. One habit worth forming early: name the cells that hold your inputs, so the formula reads in plain language instead of a string of cell addresses. A reviewer — or you in three months — can then follow the logic without decoding what B7 and D2 were supposed to mean, which is most of what makes a sheet maintainable.
In Google Sheets
Google Sheets handles this almost identically to Excel. The formula syntax above is the same, and the menu lives under a slightly different label rather than a ribbon tab. Use the platform toggle at the top of the page to switch every keyboard shortcut between Windows and Mac, and expect at most cosmetic differences in naming. Keep this page bookmarked for the next time the same question comes up. Better still, rebuild the example once in your own sheet — doing it yourself, with the tool above to check against, is what turns a copied formula into a technique you own. Treat “excel vba remove duplicates” as a small building block rather than a chore. Once the inputs sit in their own cells and the formula reads from them, the same setup answers a dozen related questions with a tweak, and Excel keeps every dependent figure current as the data changes. The tool above is there so you can rehearse and verify before committing anything to a real workbook; the steps and worked example are there so the logic sticks. Get it right once and it stops costing you time — it starts saving it, every time the question comes back around.
Common mistakes
- Saving as .xlsx instead of .xlsm — Excel strips all VBA code when you save in the non-macro-enabled format. Always choose "Excel Macro-Enabled Workbook (.xlsm)" from the Save As dialog.
- Running the macro on the wrong sheet — Sub RemoveDuplicatesVBA() acts on ActiveSheet by default. If you have multiple sheets open, click the correct sheet tab first before pressing Run.
- Macros are disabled — if you see a security warning bar, click Enable Content. If there is no bar at all, set File ▸ Options ▸ Trust Center ▸ Trust Center Settings ▸ Macro Settings to "Disable VBA macros with notification", which is the safe default that still lets you approve a file per workbook. Do not select "Enable all macros": it removes the only warning you get before a hostile workbook executes.
Frequently asked questions
How do I run the Remove Duplicates with VBA macro?
Press Alt + F11 to open the VBA editor, insert a Module (Insert ▸ Module), paste Sub RemoveDuplicatesVBA() into the code pane, and press F5. Alternatively close the editor and run it from Developer ▸ Macros ▸ select "RemoveDuplicatesVBA" ▸ Run.
Why won't my macro run — macros are disabled?
Excel blocks macros by default for files downloaded from the internet. Click the yellow security bar and choose "Enable Content" — that trusts this one workbook and is all that is normally needed. Leave File ▸ Options ▸ Trust Center ▸ Trust Center Settings ▸ Macro Settings on "Disable VBA macros with notification" rather than switching to "Enable all macros", which Microsoft itself marks not recommended. If the bar is red instead of yellow, the file carries Mark of the Web: close Excel, right-click the file, open Properties and tick Unblock. Also ensure the file is saved as .xlsm, not .xlsx.
Can I undo what the Remove Duplicates with VBA macro did?
No — VBA actions generally cannot be undone with Ctrl+Z because they bypass Excel's undo stack. Always test Sub RemoveDuplicatesVBA() on a backup copy of the workbook before running it on live data, or add explicit validation logic inside the macro before the destructive step.