Sub topic 8
Review Duplicates Before Deleting Them with COUNTIF
This chapter assumes you can enter formulas, use absolute references like $A$2, and have already trimmed and cleaned your text. Deleting duplicates is hard to undo, and a formula that marks them lets you see what would be removed before you remove it.
Why flag first
The Remove duplicates tool (Data > Data cleanup > Remove duplicates) deletes rows immediately. It keeps the first occurrence in each group and removes the rest. That is fine when you are sure what counts as a duplicate. It is risky when you are not, for two reasons:
- Two rows can share a key, such as an email address, and still hold different information in other columns.
- Two rows that look identical may not match, and two that look different may.
A helper column built with COUNTIF puts a visible TRUE or FALSE next to each row. You can filter on it, sort it, count it, and compare it to what you expected. Nothing in your data changes.
The running COUNTIF formula
Put this in a helper column next to your data, in the first data row (row 2 if row 1 holds headers), and fill it down:
=COUNTIF($A$2:A2, A2) > 1The range $A$2:A2 is anchored at the top and ends at the current row, so it grows as you fill down. In row 5 the formula counts how many times the value in A5 appears in A2 through A5. It returns TRUE only when that count is above 1, meaning the value has already appeared higher up.
This is why the first appearance of a value is FALSE and every later appearance is TRUE. That matches what Remove duplicates would keep and delete, so the helper column is a preview of the tool's result.
A worked example
Here is a small email list, with the helper column filled down:
| Row | A (email) | Helper result | Why |
|---|---|---|---|
| 2 | [email protected] | FALSE | First appearance |
| 3 | [email protected] | TRUE | Matches row 2, because COUNTIF ignores case |
| 4 | [email protected] (trailing space) | FALSE | The extra space makes it a different string |
| 5 | [email protected] | FALSE | First appearance |
| 6 | [email protected] | TRUE | Third match for the value above |
| 7 | [email protected] | TRUE | Second appearance |
Row 4 is the instructive one. To a person it is a duplicate, but the formula says FALSE. Run TRIM and CLEAN on the key column first (the earlier chapters cover this), then flag. Otherwise duplicates survive and you get a false sense that the data is clean.
Variations you will use
Flag every member of a duplicate group
The running formula never marks the first occurrence. To see all rows that belong to a group, count over the whole column instead:
=COUNTIF($A$2:$A, A2) > 1Both versions give TRUE for the later repeats. This one also gives TRUE for the first appearance. Use it when you need to review the whole group side by side, for example to decide which of three conflicting records to keep. Use the running version when you want to delete everything except the first.
Match on several columns
A duplicate row is often one where several fields match, not just one. COUNTIFS takes pairs of range and criterion, and every pair must match:
=COUNTIFS($A$2:A2, A2, $B$2:B2, B2, $C$2:C2, C2) > 1This flags a row only when the values in columns A, B, and C all repeat an earlier row. Rows that share an email but have a different phone number stay FALSE. That is usually what you want when records may represent distinct events.
Guard against blank rows
If the key column has blank cells, the helper may flag the blanks as repeats of each other. Wrap the test so blanks return an empty string:
=IF(A2="", "", COUNTIF($A$2:A2, A2) > 1)Count the flagged rows
A single cell above your data gives the number of rows that would be deleted:
=COUNTIF(D2:D, TRUE)Here D is the helper column. Write this number down before deleting. After the deletion it is your check.
Review the flagged rows
Once the helper column is filled, work through these steps:
- Select the data, including headers, and choose Data > Create a filter.
- Open the filter on the helper column and show only TRUE.
- Scan the visible rows. Do the other columns agree with the earlier row, or do they hold different information?
- Sort or filter by the key column if you want to see the original and its repeats together. For that, use the whole-column variant.
- Optionally highlight duplicates without a helper column. Choose Format > Conditional formatting, pick "Custom formula is", and enter
=COUNTIF($A$2:$A, $A2) > 1.
Gotchas that cause wrong flags
COUNTIF is built for criteria such as ">5" or "a*", not for exact comparison. That produces some edge cases.
- Case is ignored. "[email protected]" and "[email protected]" count as the same value. Often you want that. If case matters, such as with case-sensitive product codes, use an exact comparison:
=SUMPRODUCT(EXACT($A$2:A2, A2)) > 1EXACT compares case-sensitively, and SUMPRODUCT adds up the TRUE results.
- Wildcards are active. In COUNTIF criteria,
*matches any run of characters and?matches one character. A value such as "SKU-1?" or "a*b" may match other rows. Prefix a literal wildcard with a tilde, as in~?, or use the SUMPRODUCT and EXACT version, which treats these characters as plain text. - Leading operators are read as operators. A cell whose text starts with
>,<, or=can be interpreted as a comparison instead of text. - Numbers and numeric text can match. The text "123" and the number 123 may be counted together, and leading zeros stored as text, such as "00123", can behave unexpectedly. Check how the key column is stored before relying on matching.
- Hidden characters defeat matching. Non-breaking spaces (CHAR(160)) are not removed by TRIM. If an obvious pair is not flagged, clean the column with SUBSTITUTE and CLEAN first.
- Performance. The running range makes each row scan every row above it, so cost grows with the square of the row count. On tens of thousands of rows, a helper column can slow the sheet. In that case, use UNIQUE in a separate range.
Delete and verify
When the TRUE rows are confirmed duplicates, you have two options. You can select the filtered rows and delete them with right-click > Delete selected rows. In a filtered view, Sheets deletes only the visible rows, but confirm this on your copy first. Or you can remove the filter and run Data > Data cleanup > Remove duplicates, choosing the same key columns you used in the formula.
Then compare the numbers:
- Original row count minus the flagged count should equal the new row count.
- Re-run the helper formula on the result. Every cell should be FALSE, and the flagged count should be 0.
- Spot-check a few groups that had conflicting values, and confirm the surviving row is the one you meant to keep.
If you would rather leave the source untouched, the UNIQUE function produces a deduplicated list elsewhere. The helper column still helps there, because it tells you what UNIQUE should drop.
Exercise
A sheet has emails in A2:A500. You fill =COUNTIF($A$2:A2, A2) > 1 down column B and 40 cells show TRUE. You delete those 40 rows, but a colleague says a few duplicates remain. What are the likely causes, and how would you check?
Show answer
COUNTIF only matches strings that are identical apart from case, so duplicates that differ by a leading or trailing space or by a hidden character such as a non-breaking space (CHAR(160)) were never flagged. To check, re-run the helper after cleaning the column with =ARRAYFORMULA(TRIM(CLEAN(SUBSTITUTE(A2:A, CHAR(160), " ")))) and see whether new TRUE values appear. Also confirm that the rows are really duplicates under the same key columns, and use COUNTIFS if several columns define a duplicate.
Exercise
Write a formula for row 2 that flags a row as a duplicate only when both the email in column A and the date in column C repeat an earlier row. Then say how it differs from =COUNTIF($A$2:$A, A2) > 1.
Show answer
=COUNTIFS($A$2:A2, A2, $C$2:C2, C2) > 1. It uses a running range, so the first occurrence stays FALSE and only later repeats are TRUE. It requires both columns to match, so two rows with the same email but different dates are not flagged. The whole-column COUNTIF version looks only at column A and marks every member of a group, including the first.
Exercise
Your key column holds case-sensitive codes, so "ab12" and "AB12" are different products. COUNTIF flags them as duplicates. What formula avoids this?
Show answer
=SUMPRODUCT(EXACT($A$2:A2, A2)) > 1. EXACT compares text case-sensitively, so "ab12" and "AB12" are not counted together, and SUMPRODUCT adds up the exact matches in the running range.