Excel guides

Count Unique Values in Excel

Count unique values in Excel with COUNTA and UNIQUE in Excel 365 and 2021, or SUMPRODUCT and COUNTIF in older versions, and handle blanks and spaces.

By Nicolaj, founder of ExcelSavvy · · 10 min read

A laptop on a bright desk by a large window, its screen showing a spreadsheet-style table with one highlighted row.

If you're on Excel 365 or Excel 2021 or later, use =COUNTA(UNIQUE(B2:B13)). On older versions it's =SUMPRODUCT(1/COUNTIF(B2:B13,B2:B13)). In both, swap B2:B13 for your own column.

Watch out, though. Both formulas give you the wrong number if the range has a blank cell in it, or if a name has a stray space. Every formula in this guide runs on the same small orders sheet, and we tell you the number it should return, so you can check yourself as you go.

The formula that counts unique values

Counting unique values means you count each different value once. Microsoft's example is a column with 5, 6, 7 and 6, which has three unique values: 5, 6 and 7.

In Excel 365 and Excel 2021 or later, the formula is =COUNTA(UNIQUE(B2:B13)). The UNIQUE function lists each value once and COUNTA counts that list (ROWS works in place of COUNTA too, if you'd rather use it).

Excel 2019 and older don't have UNIQUE, because it's only in Excel 2021+ and Excel 365. So there you use =SUMPRODUCT(1/COUNTIF(B2:B13,B2:B13)) instead. Both formulas need a small fix when the range has blank cells, and we'll get to that further down.

The sample sheet used in this guide

Type this into a blank sheet so your numbers match ours. The headers Order ID, Customer and Region go in A1:C1, and the data runs from row 2 down to row 13. We've left Order ID out of the table below because none of the formulas touch it. They only use Customer and Region.

RowB: CustomerC: Region
2AcmeNorth
3BoltSouth
4AcmeNorth
5CrestNorth
6BoltSouth
7DeltaNorth
8AcmeSouth
9CrestNorth
10(blank)South
11EchoNorth
12BoltSouth
13DeltaSouth

These are the answers you should end up with, so you can check your work: 5 different customers (Acme, Bolt, Crest, Delta and Echo), 4 different customers in the North region, and 1 customer who shows up exactly once, which is Echo.

Count unique values with UNIQUE in Excel 365 and 2021

Use this method if you have Excel 365 or 2021 or later. It updates on its own when the data changes, and you can read the formula at a glance.

Step 1. List each value once with UNIQUE

Click E2, type =UNIQUE(B2:B13) and press Enter. The list spills down the column: Acme, Bolt, Crest, Delta, a 0, and Echo. That 0 is the blank cell, which UNIQUE picked up as a value. So have a look at the spilled list before you count it, because it's the easiest place to catch something odd like that.

Step 2. Wrap it in COUNTA to get the number

Now wrap it: =COUNTA(UNIQUE(B2:B13)). On the sample sheet you get 6, not 5, because the blank gets counted as well. =ROWS(UNIQUE(B2:B13)) gives 6 too. And if your list is already sitting in E2, =COUNTA(E2#) counts the spilled range directly.

On versions before 2021 you'll see #NAME? instead, since UNIQUE doesn't exist there. In that case skip ahead to the COUNTIF method in the next section.

Step 3. Leave blank cells out of the count

The fix is to filter out the blanks before UNIQUE ever sees them:

=COUNTA(UNIQUE(FILTER(B2:B13,B2:B13<>"")))

That gives 5. The bit B2:B13<>"" means "keep only cells that are not empty".

You can also write it as =SUM(--(LEN(UNIQUE(B2:B13))>0)). This one only counts items with at least one character in them, so the blank falls out and you get 5 again. The -- turns TRUE and FALSE into 1 and 0 so SUM has numbers to add. It looks fussier, but it earns its place later: once you add a condition that no row matches, this version still gets it right (more on that two sections down).

If you want to practice the list part on its own, try sorting a unique list.

Count unique values in older Excel versions

The formula is =SUMPRODUCT(1/COUNTIF(B2:B13,B2:B13)). COUNTIF tells each row how many times its value appears. Acme appears 3 times, so each Acme row adds 1/3, and the three rows together add 1. That way every customer ends up adding exactly 1, however often it shows up.

On the sample, though, you'll get #DIV/0!. COUNTIF returns 0 for the blank cell and 1/0 is an error, so use this version instead:

=SUMPRODUCT((B2:B13<>"")/COUNTIF(B2:B13,B2:B13&""))

This returns 5. The &"" stops the zero, and (B2:B13<>"") gives the blank row a weight of 0.

You'll also run into the same idea written with SUM instead of SUMPRODUCT, or as Microsoft's FREQUENCY and MATCH version. Those are array formulas. Current Microsoft 365 only needs Enter, but older versions need Ctrl+Shift+Enter or the result is wrong. SUMPRODUCT doesn't need any of that, so it's the one we recommend here.

If you'd like to get a feel for COUNTIF first, try the COUNTIF text exercise.

Count unique values that meet a condition

Say you want to know how many different customers bought in North. Use:

=SUM(--(LEN(UNIQUE(FILTER(B2:B13,C2:C13="North","")))>0))

You get 4: Acme, Crest, Delta and Echo. This is the UNIQUE, LEN and FILTER combination, with the FILTER function doing the narrowing down. Exceljet's worked example uses the same pattern (it returns 3 for its own criterion).

So why not just COUNTA(UNIQUE(FILTER(...)))? Because when no row matches, FILTER hands back the "" you gave it, and COUNTA counts that as 1. The LEN test gives 0, which is right. It's also worth putting the region in a cell, say G1, and writing C2:C13=G1, so you can reuse the formula for any region without editing it.

For older versions:

=SUMPRODUCT((C2:C13="North")*(B2:B13<>"")/COUNTIFS(B2:B13,B2:B13&"",C2:C13,C2:C13&""))

That returns 4 as well. Each row gets divided by the number of rows that share both its customer and its region, and any row outside North is multiplied by 0. It's a long one, so copy it carefully.

To practice both halves, try the FILTER one-condition exercise for the narrowing and the COUNTIFS two-conditions exercise for the counting.

Distinct values or values that appear only once?

There are two meanings here and they're easy to mix up. A distinct count counts each different value once, which is 5 on the sample. A count of values that appear exactly once is 1, because Echo is the only customer that does. Most searches for "unique" mean the distinct count, and that's how Microsoft defines it too.

If you do want the exactly-once count, use UNIQUE's third argument:

=COUNTA(UNIQUE(FILTER(B2:B13,B2:B13<>""),,TRUE))

That returns 1. Leave the FILTER out, though, and the blank cell "appears once" as well, so you get 2. In older versions =SUMPRODUCT((COUNTIF(B2:B13,B2:B13&"")=1)*(B2:B13<>"")) does the same job and returns 1.

Why your unique count comes out wrong

Blank cells. A blank adds 1 to the UNIQUE count, and in the plain COUNTIF version it gives you #DIV/0!. The fixes are in the sections above.

Extra spaces. Say B4 holds "Acme " with a space at the end. Excel now sees a sixth customer, and you'll struggle to spot it just by looking at the cell. Nicolaj, who built ExcelSavvy's formula engine, notes that details like an extra space decide whether a lookup or a count matches. Either clean the column first or put TRIM inside the count:

=COUNTA(UNIQUE(TRIM(FILTER(B2:B13,B2:B13<>""))))

There's an exercise to remove extra spaces with TRIM if you want some practice.

Capital letters. UNIQUE is not case-sensitive, so ACME, Acme and acme all count as one value, and COUNTIF works the same way. For customer names that's usually what you want anyway.

When case does matter (product codes, for example), you need this Excel 365 formula:

=LET(r,FILTER(B2:B13,B2:B13<>""),SUM(1/MMULT(--EXACT(r,TRANSPOSE(r)),SEQUENCE(ROWS(r),1,1,0))))

It's the fiddliest formula in this guide, so take it piece by piece:

  • r is the filtered list, with the blanks removed.
  • EXACT compares every value with every other value, and it does match case.
  • MMULT adds up the exact matches for each row.
  • SUM adds 1 divided by each of those counts, so each distinct value contributes 1.

Count unique values without a formula

Advanced Filter. Select B1:B13 and go to Data > Advanced. Choose Copy to another location, tick Unique records only, then pick a target cell. After that you count the copied list with ROWS, as Microsoft describes. If the copy lands in E1, the list minus its header is E2:E7, so that's =ROWS(E2:E7). Check the copied list before you count, in case a blank row came along with it. And keep in mind the copy is a snapshot, so it won't update when your data changes.

Filter versus Remove Duplicates. Filtering for unique values only hides duplicates temporarily. Remove Duplicates deletes them for good, so if all you want is a count, run it on a copy. Both tools compare what the cell shows rather than the stored value, which means the same date in two formats counts as two.

PivotTable. As you insert the PivotTable, tick "Add this data to the Data Model". Then drag Customer to Values and choose Value Field Settings > Distinct Count. If you didn't tick that box you'll only see Count, which counts rows and not different customers (easy to miss). If you haven't made one before, start with how to build a pivot table.

Keep the count right when the list grows

A fixed range like B2:B13 won't pick up anything you type in row 14. So turn the data into a table with Ctrl+T, then type Orders in the Table Name box on the Table Design tab. Don't skip that bit, because otherwise the table is called Table1 and the formula below fails. Then write:

=COUNTA(UNIQUE(FILTER(Orders[Customer],Orders[Customer]<>"")))

Orders[Customer] works because B1 holds the header Customer, and the range now grows along with the table. In older versions the SUMPRODUCT formula takes Orders[Customer] too.

This matters at work and in tests, because a good answer is a formula that keeps working when the list changes, and a number you typed in won't. ExcelSavvy's admin test works that way. Nothing is marked until you hand in, and your answers have to be formulas that survive a changed list.

Counting unique values in Google Sheets

Google Sheets has one function that does the whole thing: =COUNTUNIQUE(B2:B13) (we have a page on COUNTUNIQUE). =COUNTA(UNIQUE(B2:B13)) works there as well, and so do the SUMPRODUCT patterns above.

Blank cells and extra spaces throw off the count in Sheets too. List the values with UNIQUE first, look at what comes back, and only then trust the number. TRIM cleans up spaces the same way it does in Excel.

Practice counting unique values yourself

In short:

  • Excel 365 and 2021: COUNTA(UNIQUE()).
  • Older versions: SUMPRODUCT(1/COUNTIF()).
  • Deal with blanks and spaces before you trust the number.

In the exercise you list each customer once with UNIQUE. You type the formula yourself, in a real browser spreadsheet that behaves like Excel, and you move on once it works. Stuck? Cellie gives you a hint about what went wrong. You can start without an account and make a free one later if you want to save your progress. It all runs in the browser, so you don't need Excel installed.

There are 173 exercises on ExcelSavvy. Finish a topic with premium and you get a certificate with your name on it and a link anyone can check. You can put the Excel certificate on a résumé or LinkedIn.

FAQ

How do I use COUNTIF but leave out duplicates?

Divide 1 by the COUNTIF result for each row, then add it all up: =SUMPRODUCT((B2:B13<>"")/COUNTIF(B2:B13,B2:B13&"")). That way each value adds exactly 1, no matter how often it appears. On the sample sheet you get 5.

Can I use COUNTIF with UNIQUE?

Usually there's no need. Wrapping COUNTA or ROWS around UNIQUE already gives you the count. If you need a condition, put FILTER inside UNIQUE, like =SUM(--(LEN(UNIQUE(FILTER(B2:B13,C2:C13="North","")))>0)), which returns 4.

How do I count unique values based on another column?

Filter by the other column first, and then count what's unique in what's left. In older versions you'd use SUMPRODUCT with COUNTIFS on both columns. On the sample sheet both give 4 for North.

Why is my unique count one higher than expected?

Most of the time it's a blank cell, or a value with an extra space in it. Filter the blanks out with FILTER(range,range<>"") and clean the spaces with TRIM.

Is the unique count case-sensitive?

No. UNIQUE and COUNTIF see Apple and APPLE as the same value. If case has to matter, use the EXACT-based formula from earlier.

Practice every counting and lookup formula in a real browser spreadsheet, and earn a certificate you can share. Premium is $19 a month or $59 a year, with a full refund within 14 days.

Nicolaj

About the author

Nicolaj, founder of ExcelSavvy

I'm a software developer, and I built ExcelSavvy from scratch, including the spreadsheet itself and the engine that calculates the formulas. To make that work, I had to understand every function down to the details: how VLOOKUP behaves when a code has one space too many, why a date is really a number, and when ROUND gives a different answer than the number format.

More about Nicolaj

Practice what this guide covers

Start with the first exercise in a real grid. Every answer is checked right away.