Excel like a finance pro.
Strip duplicate rows (and count what you removed)
Exports double up. The useful version keys on the right columns and says how many went.
1What it does
An export that ran twice doubles every row, and a duplicated invoice overstates revenue without anything looking wrong. `Range.RemoveDuplicates` is one line; the useful version around it does two more things. It keys on the columns that actually define a duplicate — the invoice number, not the whole row, which can differ by a timestamp — and it counts the rows before and after, so you know how much it removed. A dedupe that doesn't report its count is just unexplained data loss.
2The code
Sub DedupeInvoices()
Dim ws As Worksheet, rng As Range
Dim before As Long, after As Long, lastRow As Long, lastCol As Long
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
If lastRow < 3 Then Exit Sub
Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol))
before = lastRow - 1
' Key on column 1 (invoice number) only. Array(1, 3) would key on two.
' Header:=xlYes stops the header row being treated as data.
rng.RemoveDuplicates Columns:=Array(1), Header:=xlYes
after = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row - 1
MsgBox "Removed " & (before - after) & " duplicate row(s)." & vbNewLine & _
before & " rows before, " & after & " after.", vbInformation
End SubPaste into the Visual Basic Editor (Alt + F11 → Insert → Module), then run and save as macro-enabled (.xlsm).
New to macros? Set up in 5 minutes▾
- 1
Don't see the Developer tab in the ribbon?
You don't strictly need it — Alt + F11 opens the editor directly — but it makes running macros easier.- Windows: File → Options → Customize Ribbon → tick Developer in the right-hand list → OK.
- Mac: Excel → Preferences → Ribbon & Toolbar → tick Developer → Save.
- 2
Paste in the code
Press Alt + F11 to open the Visual Basic editor, then Insert → Module and paste the snippet's code into the blank window. Close it with Alt + Q. - 3
Run it
Press Alt + F8, pick the macro's name, and click Run — that's it. (Pasted a custom function instead? Just type it into a cell like any built-in:=GrossMargin(B2, B3).) - 4
Keep the macro — save as .xlsm
File → Save As → Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx silently drops the code when you save. - 5
Macros blocked?
Click Enable Content on the yellow bar. If you downloaded the file, you may first need to right-click it → Properties → tick Unblock → OK, then reopen.
Heads up: macros can't be undone with Ctrl + Z — save a copy before running one that changes your workbook.
3When you use it
- Clean an export that ran twice.
- Reduce a transaction list to one row per invoice.
- Build a unique customer list from a year of orders.
4See it in action
A simulation — press Run to perform what the macro does to a sample workbook.
| INV-1041 · Acme | 09:14 |
| INV-1042 · Globex | 09:14 |
| INV-1041 · Acme | 09:31 |
| INV-1042 · Globex | 09:31 |
| INV-1043 · Initech | 09:31 |
Two invoices appear twice, from two runs a few minutes apart.
5Pitfalls
Keying on every column misses real duplicates that differ only by an import timestamp.
Fix: Pass Columns:=Array(1) — the identifying column — rather than all of them.
Keying too loosely deletes legitimate rows that merely share a value.
Fix: Test on a copy first, and read the count. If it removed more than expected, the key is too broad.
The header row is treated as data and removed or used as a key.
Fix: Header:=xlYes, always, for a range with headings.
Passing a VBA variable as the Columns argument fails with error 5.
Fix: It needs a real array — Array(1, 3) — or an Evaluate trick for a dynamic list. A variable holding numbers isn't accepted.
6No-code alternatives
- Data → Remove Duplicates — The same thing by hand, and it reports the count too. The macro is for doing it every month without thinking. See the tip.
- UNIQUE — Returns a de-duplicated copy without touching the source — safer when you want to keep the original.
Rather not write macros? Wauvel's free tools generate branded, formula-driven Excel for you — no VBA required.
Learn the moves here — or let Wauvel run them on your numbers.
Meet your AI CFO →One CFO-grade Excel tip a week
A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.