Menu

Count Unique Values in Excel: UNIQUE and COUNTIF Formulas

=COUNTA(UNIQUE(A2:A9)) counts how many different values are in A2:A9. For older Excel use =SUMPRODUCT(1/COUNTIF(A2:A9,A2:A9)). Count values that appear once, count with a condition, and skip blanks.

Every sheet on this page is live: change a number or a formula and it recalculates.

=COUNTA(UNIQUE(A2:A9)) counts how many different values are in A2:A9. UNIQUE returns each value once, and COUNTA counts that list. It needs Excel 2021 or Microsoft 365; older versions are covered below.

Different customers
D2
ABCD
1CustomerUnique listCount
2AnaAna5
3BenBen
4AnaCara
5CaraDan
6BenEva
7Dan
8Ana
9Eva
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Eight orders came from five customers. C2 spills the list of UNIQUE names so you can see what is being counted, and D2 counts it without needing the list on the sheet. Change A9 to Ana and the count drops to 4; type a new name and it goes up.

UNIQUE ignores case, so Ana and ana count as one customer.

Count unique values in older Excel

Excel 2019 and earlier have no UNIQUE. The classic formula is:

=SUMPRODUCT(1/COUNTIF(A2:A9,A2:A9))

COUNTIF with the whole range as its criteria returns, for every row, how many times that row's value appears. A name that appears 3 times gets 3 on each of its rows, so 1/3 is added three times and the name adds up to exactly 1. Column B shows the count for each row and column C the fraction.

How 1/COUNTIF works
E2
ABCDE
1CustomerTimes1/TimesCount
2Ana30.335
3Ben20.505.00
4Ana30.33
5Cara11.00
6Ben20.50
7Dan11.00
8Ana30.33
9Eva11.00
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Ana's three rows each add 0.33, Ben's two rows each add 0.50, and the three single names add 1 each: 5 in total, the same as the helper column's SUM. On tens of thousands of rows this formula is slow, because COUNTIF scans the whole range once per row; UNIQUE does not have that cost.

Distinct vs unique: values that appear only once

"Unique" is used for two different counts. The one above counts distinct values: every name once. The other counts the values that appear exactly once, such as customers who ordered only one time. UNIQUE does that with its third argument, exactly_once, set to TRUE.

Distinct and exactly once
D3
ABCD
1CustomerCountResult
2AnaDistinct5
3BenExactly once3
4AnaExactly once, older Excel3
5Cara
6Ben
7Dan
8Ana
9Eva
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Five distinct customers, but only three of them, Cara, Dan and Eva, ordered once. The older-Excel version counts the rows whose COUNTIF is exactly 1. If every value repeats, UNIQUE with exactly_once returns #CALC! and COUNTA counts that error as 1; the SUMPRODUCT version gives 0.

Count unique values with a condition

To count the different customers in one region, filter the rows first and then count what is left. FILTER keeps the North rows and UNIQUE removes the repeats.

Different customers per region
E2
ABCDE
1CustomerRegionRegionCustomers
2AnaNorthNorth3
3BenSouthSouth3
4AnaNorthNorth, older Excel3
5CaraNorth
6BenNorth
7DanSouth
8AnaNorth
9EvaSouth
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

North has five orders from three customers: Ana, Cara and Ben. E3 counts South the same way. E4 is the version for Excel 2019 and earlier: COUNTIFS counts each customer and region pair, and the condition keeps only the North fractions.

If no row matches, FILTER returns #CALC!, and COUNTA counts that error as one value: type West in D2 and E2 shows 1, not 0. Wrapping the formula in IFERROR does not help, because COUNTA returns no error. Count the rows of the result instead, which does pass the error on: =IFERROR(ROWS(UNIQUE(FILTER(A2:A9,B2:B9="West"))),0) returns 0.

Count unique values and ignore blanks

An empty cell in the range becomes one more "value". UNIQUE returns it as a 0 and COUNTA counts that 0, so for Ana, an empty cell, Ben, Ana, an empty cell, Cara and Ben, Excel gives:

=COUNTA(UNIQUE(A2:A8))    4   three names plus the 0 for the empty cells

In the older formula an empty row makes COUNTIF return 0, so 1/0 gives #DIV/0!. Remove the blanks first:

A range with gaps
D2
ABCD
1CustomerFormulaCount
2AnaSkip blanks3
3Older Excel3
4Ben
5Ana
6
7Cara
8Ben
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Both formulas count the three customers. FILTER with A2:A8<>"" drops the empty cells before UNIQUE sees them. In the older formula, A2:A8&"" turns each empty cell into an empty string so COUNTIF never returns 0, and (A2:A8<>"") gives those rows a weight of 0.

Practice: count the products

Your turn: how many products?
E2
ABCDE
1OrderProductCountResult
21001AppleProducts
31002Pear
41003Apple
51004Plum
61005Pear
71006Apple
81007Plum
91008Fig
Click a cell to see its formula. Change a number or a formula and the sheet recalculates.

Your turn: Count how many different products appear in B2:B9. Write the formula in E2.

Which formula for your Excel

CountExcel 365 / 2021Excel 2019 and earlier
Distinct values=COUNTA(UNIQUE(A2:A9))=SUMPRODUCT(1/COUNTIF(A2:A9,A2:A9))
Values that appear once=COUNTA(UNIQUE(A2:A9,,TRUE))=SUMPRODUCT(--(COUNTIF(A2:A9,A2:A9)=1))
Distinct, with a condition=COUNTA(UNIQUE(FILTER(A2:A9,B2:B9="North")))=SUMPRODUCT((B2:B9="North")/COUNTIFS(A2:A9,A2:A9,B2:B9,B2:B9))
Distinct, skipping blanks=COUNTA(UNIQUE(FILTER(A2:A9,A2:A9<>"")))=SUMPRODUCT((A2:A9<>"")/COUNTIF(A2:A9,A2:A9&""))

In a pivot table, the Distinct Count summary does the same job without a formula, but only when the pivot table is created with "Add this data to the Data Model" ticked. To delete the repeats rather than count them, see remove duplicates.

Frequently Asked Questions

How do I count unique values in Excel?

In Excel 365 or 2021, use =COUNTA(UNIQUE(A2:A9)): UNIQUE lists each value once and COUNTA counts the list. In older versions use =SUMPRODUCT(1/COUNTIF(A2:A9,A2:A9)).

How do I count values that appear only once?

Set UNIQUE's third argument, exactly_once, to TRUE: =COUNTA(UNIQUE(A2:A9,,TRUE)). For Ana, Ana, Ben it gives 1, since only Ben appears once. In Excel 2019 and earlier use =SUMPRODUCT(--(COUNTIF(A2:A9,A2:A9)=1)).

How do I count unique values with a condition?

Filter first, then count: =COUNTA(UNIQUE(FILTER(A2:A9,B2:B9="North"))) counts the different customers in the North rows. If no row matches, COUNTA counts FILTER's #CALC! error as 1, so when that can happen use =IFERROR(ROWS(UNIQUE(FILTER(A2:A9,B2:B9="North"))),0).

How do I count unique values and ignore blank cells?

Remove the blanks before UNIQUE: =COUNTA(UNIQUE(FILTER(A2:A9,A2:A9<>""))). In older Excel, =SUMPRODUCT((A2:A9<>"")/COUNTIF(A2:A9,A2:A9&"")) skips them.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED