How-to recipes · Counting and summing

How to count unique values in Excel

Count how many different values a list contains with UNIQUE and COUNTA, or with SUMPRODUCT and COUNTIF in Excel 2016 and 2019.

Beginner UNIQUE COUNTA ROWS SUMPRODUCT COUNTIF FILTER

You have a column of customer names taken from an order list, and the same customer appears many times because they ordered more than once. Your manager asks a simple question: how many different customers did we sell to? Counting the rows gives the number of orders, not customers. What you need is the number of distinct values, where every name counts once no matter how often it repeats. This recipe shows the quickest way in Microsoft 365, a formula that works in Excel 2016 and 2019, and the traps that make the count wrong.

The example data

The orders are in A1:A9 with a header in A1. Alpha Ltd ordered three times, Brightway twice, and the other three customers once each, so the answer we expect is 5.

Microsoft 365 and Excel 2021: UNIQUE + COUNTA

UNIQUE returns the list without repeats, and COUNTA counts how many items that list holds. Put them together in any empty cell:

C2fx=COUNTA(UNIQUE(A2:A9))
ABC
1Customer
2Alpha Ltd5
3Brightway
4Alpha Ltd
5Cedar Co
6Brightway
7Delta Inc
8Alpha Ltd
9Evergreen

UNIQUE produces Alpha Ltd, Brightway, Cedar Co, Delta Inc and Evergreen in memory; COUNTA counts those five items.

Result: 5

You can also write =ROWS(UNIQUE(A2:A9)). ROWS counts the rows of the array instead of the non-empty items, which gives the same answer here. If you want to see the list as well as the count, type =UNIQUE(A2:A9) in one cell so it spills down, and =COUNTA(C2#) next to it, where C2# means the whole spilled range.

Distinct versus unique

People use the word unique for two different questions. Distinct means every different value once (the answer above, 5). Unique in the strict sense means values that appear exactly once. UNIQUE can answer the second question too through its third argument, exactly_once:

C2fx=COUNTA(UNIQUE(A2:A9,,TRUE))
ABC
1Customer
2Alpha Ltd3
3Brightway
4Alpha Ltd
5Cedar Co
6Brightway
7Delta Inc
8Alpha Ltd
9Evergreen

Only Cedar Co, Delta Inc and Evergreen occur a single time, so the result is 3.

Result: 3

Excel 2016 and 2019: SUMPRODUCT + COUNTIF

Older versions have no UNIQUE function, but a classic formula does the same job without any helper columns:

C2fx=SUMPRODUCT(1/COUNTIF(A2:A9,A2:A9))
ABC
1Customer
2Alpha Ltd5
3Brightway
4Alpha Ltd
5Cedar Co
6Brightway
7Delta Inc
8Alpha Ltd
9Evergreen

Works in every version of Excel since 2007.

Result: 5

How it works, step by step:

  1. COUNTIF(A2:A9,A2:A9) asks, for every row, how many times that row's value appears in the whole list. The result is the array {3;2;3;1;2;1;3;1}.
  2. 1/... turns each count into a fraction: each Alpha Ltd row becomes 1/3, each Brightway row 1/2, and the single customers 1.
  3. The fractions of one customer always add up to exactly 1 (three thirds, two halves), so SUMPRODUCT adding everything returns the number of different customers: 5.

SUMPRODUCT is used instead of SUM because it handles arrays without Ctrl+Shift+Enter in older Excel. For the strict appears once count in Excel 2016, use =SUMPRODUCT(--(COUNTIF(A2:A9,A2:A9)=1)), which counts the rows whose value has a count of exactly 1.

Blank cells break both formulas

Watch out. If the range contains empty cells, 1/COUNTIF(...) divides by zero for the blanks and the whole SUMPRODUCT returns #DIV/0!. UNIQUE, on the other hand, keeps one blank as a value of its own (shown as 0), so COUNTA counts it and your answer is one too high.

Use these versions when the column may contain gaps:

Formula
=COUNTA(UNIQUE(FILTER(A2:A100,A2:A100<>"")))
Microsoft 365: FILTER removes the blanks before UNIQUE sees them.
Formula
=SUMPRODUCT((A2:A100<>"")/COUNTIF(A2:A100,A2:A100&""))
Excel 2016/2019: blanks get a numerator of 0, and &"" stops the division by zero.

Other ways to get the count

Pitfalls

  • Both UNIQUE and COUNTIF ignore case, so alpha ltd and Alpha Ltd count as one customer.
  • A trailing space makes Alpha Ltd a different value. Clean the column with TRIM first if the data comes from another system.
  • Numbers stored as text ("105") and real numbers (105) are different values for UNIQUE, but COUNTIF treats them as the same. Convert everything to one type before counting.
  • COUNTIF-based formulas get slow on tens of thousands of rows because every row is compared with every other row. UNIQUE is much faster on big lists.
Tip. Point the formula at an Excel table column, for example =COUNTA(UNIQUE(Orders[Customer])), and the count updates automatically when new orders are added. Tables are explained in Excel tables and structured references.

Related reading: Dynamic arrays: spill, FILTER, SORT, UNIQUE, SEQUENCE and # explains spilled formulas like UNIQUE, and Counting and summing with criteria: COUNTIFS, SUMIFS, SUMPRODUCT covers COUNTIF and its relatives in depth.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 16 KB

More how-to recipes