Select Count Unique? Excel Formula Explained

Formulas & functions · Updated November 22, 2026

The direct answer: In Excel, to count unique values in a range, use =SUMPRODUCT(--(FREQUENCY(MATCH(range,range,0),ROW(range)-ROW(range.firstcell)+1)>0)) for numbers and text, or the simpler =COUNTA(UNIQUE(range)) if you have Excel 365 or Excel 2021. The UNIQUE function is the modern, easiest method; the older formulas still work in any Excel version.

What Does “Count Unique” Actually Mean?

“Count unique” means counting each distinct item in a list only once, ignoring duplicates. For example, if a column contains “Apple”, “Banana”, “Apple”, “Cherry”, the unique count is 3 (Apple, Banana, Cherry). This is different from “count distinct” in some database contexts, but in Excel the two terms are used interchangeably. Excel does not have a built-in “COUNTUNIQUE” function, so you must combine functions or use the newer UNIQUE.

The Modern Way: UNIQUE + COUNTA (Excel 365/2021)

If you have Microsoft 365 or Excel 2021, the UNIQUE function returns a list of unique values, and you can wrap it in COUNTA to count them. For a range like A2:A20 containing text, numbers, or a mix:

  • Enter =COUNTA(UNIQUE(A2:A20)) in a cell.
  • Press Enter. The result is the number of unique entries.
  • If the range includes empty cells, COUNTA ignores them, but UNIQUE may return a 0 for blanks—use =COUNTA(UNIQUE(FILTER(A2:A20,A2:A20<>""))) to exclude blanks.

Example: With A2:A7 containing “Red”, “Blue”, “Red”, “Green”, “Blue”, “Yellow”, the formula returns 4. This works on Windows, Mac, and Excel for the web. Note: UNIQUE is not available in Excel 2019 or earlier.

Classic Formula for Any Excel Version

For older Excel versions (2019 and earlier), use a combination of SUMPRODUCT, FREQUENCY, and MATCH. This works for numbers and text, but not for blank cells (it counts a blank as a unique value if present). Assume your data is in A2:A20 (no blanks). In B2, enter:

=SUMPRODUCT(--(FREQUENCY(MATCH(A2:A20,A2:A20,0),ROW(A2:A20)-ROW(A2)+1)>0))

Breakdown: MATCH returns the position of the first occurrence of each value; FREQUENCY bins those positions; the >0 test flags bins with at least one value; -- converts TRUE/FALSE to 1/0; SUMPRODUCT sums them. This is an array formula, but in modern Excel you just press Enter (no Ctrl+Shift+Enter needed). For a range starting at A2, adjust the ROW(A2) part to match your first cell.

Counting Unique Values with Conditions (COUNTIFS Alternative)

Sometimes you need to count unique values that meet a condition, like counting unique products sold by a specific salesperson. In Excel 365, use COUNTA(UNIQUE(FILTER(range, condition))). Example: Data in A2:A20 (products) and B2:B20 (salesperson). To count unique products for “John” in B2:B20, enter:

=COUNTA(UNIQUE(FILTER(A2:A20,B2:B20="John")))

For older versions, a more complex array formula is required, but a common approach is to use a helper column with IF and then count unique. Without a helper, you can use SUMPRODUCT with COUNTIFS but it’s tricky. Simpler: add a helper column C with =IF(B2="John",A2,"") and then use the classic unique count on C, but that counts blanks—so use =SUMPRODUCT((C2:C20<>"")/COUNTIF(C2:C20,C2:C20&"")) which handles blanks.

Counting Unique Numbers vs. Text

All methods above work for both numbers and text. However, be aware of formatting: Excel treats numbers and text versions of the same number (e.g., 5 and “5”) as different. Also, the FREQUENCY method works only with numeric values if you use FREQUENCY directly on the numbers, but the MATCH trick converts everything to positions, so it handles text. For a range of pure numbers, you can use a simpler formula: =SUMPRODUCT(--(FREQUENCY(A2:A20,A2:A20)>0))—this counts unique numbers directly, but it fails if there are text values. The UNIQUE method is safest for mixed data.

Common Problems and Solutions

  • Blanks counted as unique: Use FILTER to exclude blanks in 365, or in classic formulas, use =SUMPRODUCT((A2:A20<>"")/COUNTIF(A2:A20,A2:A20&"")) which ignores blanks and works in all versions.
  • #VALUE! error: This often happens with FREQUENCY if the range contains errors. Clean the data or use IFERROR in a helper column.
  • Case sensitivity: Excel counts “apple” and “Apple” as the same. If you need case-sensitive unique count, use SUMPRODUCT(--(FREQUENCY(MATCH(EXACT(A2:A20,A2:A20),MATCH(EXACT(A2:A20,A2:A20),0),0)>0)))—but that’s complex; a simpler approach is to use a helper column with =EXACT(A2,A2)—no, that doesn’t work. Actually, for case-sensitive, you can use =SUMPRODUCT(--(FREQUENCY(IF(EXACT(A2:A20,TRANSPOSE(A2:A20)),MATCH(ROW(A2:A20),ROW(A2:A20)),""),ROW(A2:A20))>0)) as an array formula (Ctrl+Shift+Enter in older versions). This is rarely needed.

Frequently Asked Questions

How do I count unique values in a pivot table?

Pivot tables don’t have a built-in “count unique” option, but you can add a helper column to your source data that marks duplicates, then count that column. For example, in a new column, use =IF(COUNTIF($A$2:A2,A2)=1,1,0) and then sum that column in the pivot table as a value field. This gives a running unique count per item.

What is the difference between COUNTIF and counting unique?

COUNTIF counts cells that meet a condition, including duplicates. For example, =COUNTIF(A2:A20,"Apple") returns the number of times “Apple” appears. Counting unique ignores duplicates and returns 1 for “Apple” if it appears at least once.

Can I count unique values with a formula that updates automatically?

Yes, all formulas above recalculate automatically when data changes. The UNIQUE function is dynamic in Excel 365, meaning it spills results that update instantly. In older versions, the classic formulas also recalculate, but they are not dynamic arrays.

Why does my UNIQUE function return a #SPILL! error?

The UNIQUE function spills results into adjacent cells. If something is in the way, you get #SPILL!. Clear the cells below or to the right of where you entered the formula, or move the formula to a location with enough empty space.

How do I count unique values across multiple columns?

If you have data in two columns, say A2:A20 and B2:B20, and you want to count unique pairs, use =COUNTA(UNIQUE(A2:A20&B2:B20)) in Excel 365. This concatenates each row’s values and then counts unique combinations. For older versions, use a helper column that concatenates with a delimiter like =A2&"|"&B2, then count unique on that helper.

Alternatives and Best Practices

If you need to count unique values frequently, consider using a PivotTable with the “Add to Data Model” option, which allows a “Distinct Count” in the value field settings. This is available in Excel 2013 and later. To do this: select your data, Insert > PivotTable, check “Add this data to the Data Model”, then drag the field to Values and change the value field settings to “Distinct Count”. This is often easier than formulas for large datasets. For a quick one-off, the UNIQUE function is fastest. Always test your formula on a small sample to ensure it handles blanks and text correctly. If you’re working with a large dataset, consider using Power Query to remove duplicates and then count rows—this is more efficient than array formulas.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.