Exceljet

Quick, clean, and to the point

Flag first duplicate in a list

Excel formula: Flag first duplicate in a list
Generic formula 
=IF(COUNTIF(A:A,A1)>1,IF(COUNTIF(A$1:A1,A1)=1,"x","xx"),"")
Explanation 

To mark the first duplicate in a list, you can use a formula based on the COUNTIF function. Optionally, you can flag subsequent duplicates with a different marker. In the example shown the formula in cell C4 is:

=IF(COUNTIF($B$4:$B$11,B4)>1,IF(COUNTIF($B$4:B4,B4)=1,"x","xx"),"")

This formula has been copied down the column, from C4 to C11.

How this formula works

At the core, this is a COUNTIF formula, wrapped in an IF statement.

Working from the outside in, IF first checks to see if the first value in the list (B4) in this case, exists more than once in the list:

=IF(COUNTIF($B$4:$B$11,B4)>1

If so, we run another COUNTIF, inside another IF.

If the value doesn't appear more than once, the IF statement returns "" (an empty string).

The second IF / COUNTIF does the main work and looks like this:

IF(COUNTIF($B$4:B4,B4)=1,"x","xx")

This part of the formula uses an expanding reference ($B$4:B4) that expands as the formula is copied down the column. (The first B4 in the range is absolute (locked), the second is relative, so it changes as the formula is copied down the list).

Remember that this part of the formula is only executed if the first COUNTIF returned a number greater than 1. So, at each row, the formula checks the count inside the range up to the current row. If the count is 1, we mark the duplicate with "x", since it's the first one we've seen. If it's not 1, we know it must be a subsequent duplicate, and we mark with "xx"

Of course, you can customize these markers any way you like.

If you don't want to mark subsequent dups at all, just use "" instead of "xx".

Author 
Dave Bruns

Excel Formula Training

Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges. You'll also learn how to troubleshoot, trace errors, and fix problems. Instant access. See details here.