Count Unique Values in a Range

To count how many different values a column contains, combine COUNTA with UNIQUE in Excel 365, use COUNTUNIQUE in Google Sheets, or SUMPRODUCT with COUNTIF in older Excel. Describe your range and conditions below and get the exact formula.

Free, no sign-up required · 5 free generations a day

Last updated

Describe your task, get the formula

Type what you need in plain English, or pick an example below. You get the formula, a step-by-step explanation, and a worked example.

Formulas for counting unique values

Syntax
=COUNTA(UNIQUE(A2:A100))
=SUMPRODUCT(1/COUNTIF(A2:A100,A2:A100))
=COUNTUNIQUE(A2:A100)

UNIQUE returns the list of different values and COUNTA counts them; this needs Excel 2021 or Microsoft 365. The SUMPRODUCT version works in every Excel version but fails on blank cells. COUNTUNIQUE is Google Sheets only.

Examples

Count distinct values, ignoring blanks

=COUNTA(UNIQUE(FILTER(A2:A100,A2:A100<>"")))

FILTER removes the empty cells first, UNIQUE keeps one of each remaining value, and COUNTA counts the result.

Count unique values that meet a condition

=COUNTA(UNIQUE(FILTER(B2:B100,A2:A100="East")))

Keeps only the rows where column A is East, then counts the different values of column B among them.

Older Excel, safe with blank cells

=SUMPRODUCT((A2:A100<>"")/COUNTIF(A2:A100,A2:A100&""))

Each value contributes 1 divided by how often it occurs, so every distinct value adds up to exactly 1. Joining an empty string stops blank cells from causing a division by zero.

Common mistakes and how to fix them

The count is one too high

A blank cell in the range is counted as a value by UNIQUE. Filter blanks out first, as in the first example.

#DIV/0! from the SUMPRODUCT formula

The range contains empty cells, so COUNTIF returns 0 for them. Use the version that joins an empty string to the criteria.

Values that look the same are counted separately

One of them has a trailing space. Clean the column with TRIM, or count UNIQUE(TRIM(A2:A100)) in Excel 365.

Frequently asked questions

What is the difference between unique and distinct?

Distinct means every different value counted once. Strictly unique means values that appear exactly once. For the second, use UNIQUE with its third argument set to TRUE: =COUNTA(UNIQUE(A2:A100,,TRUE)).

Is the count case-sensitive?

In Excel, no: apple and Apple are treated as the same value by UNIQUE and COUNTIF. In Google Sheets, UNIQUE is case-sensitive and returns them as two separate values.

Can a pivot table do this?

Yes. In Excel, add the data to the Data Model when creating the pivot table and choose Distinct Count in Value Field Settings. In Google Sheets choose COUNTUNIQUE as the summary function.

Don't want to write it by hand?

Describe what you need in plain English and get a ready-to-use formula in seconds.

Try the formula generator