42

I have an .xls file with a column with some data. How do I count how many unique values contains this column?

I have googled many options, but the formulas they give there always give me errors. For example,

=INDEX(List, MATCH(MIN(IF(COUNTIF($B$1:B1, List)=0, 1, MAX((COUNTIF(List, "<"&List)+1)*2))*(COUNTIF(List, "<"&List)+1)), COUNTIF(List, "<"&List)+1, 0))

returns enter image description here

3
  • Can you not just use a pivot table for the answer?
    – Skip Intro
    Sep 3, 2013 at 7:51
  • Actually I don't want to save the answer. I just want to check how many unique values contains the column, because this file is later imported in MySQL and I want to see if it's imported correctly. Sep 3, 2013 at 7:54
  • can you accept the most upvoted answer? I think it's better
    – greg121
    May 13, 2016 at 13:28

12 Answers 12

110

To count the number of different values in A2:A100 (not counting blanks):

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


Copied from an answer by @Ulli Schmid to What is this COUNTIF() formula doing?:

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

Counts unique cells within A1:A100, excluding blank cells and ones with an empty string ("").

How does it do that? Example:

A1:A100 = [1, 1, 2, "apple", "peach", "apple", "", "", -, -, -, ...]
then:
A1:A100&"" = ["1", "1", "2", "apple", "peach", "apple", "", "", "", "", "", ...]

so this &"" is needed to turn blank cells (-) into empty strings (""). If you were to count directly using blank cells, COUNTIF() returns 0. Using the trick, both "" and - are counted as the same:

COUNTIF(A1:A100,A1:A100) = [2, 2, 1, 2, 1, 2, 94, 94, 0, 0, 0, ...]
but:
COUNTIF(A1:A100,A1:A100&"") = [2, 2, 1, 2, 1, 2, 94, 94, 94, 94, 94, ...]

If we now want to get the count of all unique cells, excluding blanks and "", we can divide

(A1:A100<>""), which is [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, ...]

by our intermediate result, COUNTIF(A1:A100,A1:A100&""), and sum up over the values.

SUMPRODUCT((A1:A100<>"")/COUNTIF(A1:A100,A1:A100&""))  
= (1/2 + 1/2 + 1/1 + 1/2 + 1/1 + 1/2 + 0/94 + 0/94 + 0/94 + 0/94 + 0/94 + ...)
= 4

Had we used COUNTIF(A1:A100,A1:A100) instead of COUNTIF(A1:A100,A1:A100&""), then some of those 0/94 would have been 0/0. As division by zero is not allowed, we would have thrown an error.

4
  • 3
    Note: I edited again - separators are determined by regional settings, so comma works for me in UK (and also works in USA) but ; may apply elsewhere, e.g. mainland Europe Dec 4, 2013 at 14:48
  • 2
    This should be the "better" answer. Would be nice if it were marked so.
    – DynamicDan
    May 21, 2014 at 14:35
  • 3
    Works fine but damn that slows up a sheet holy crap
    – Tascalator
    Sep 22, 2014 at 15:42
  • Turns out I was trying to check for the entire column and that slows down the process so much that a macro that takes 1 second took a good 10 minutes. After checking with clients requirement the max number of rows was 156 so I set the query to check between 1 and 250. Works perfectly. Thanks houdini ;)
    – Tascalator
    Sep 22, 2014 at 19:13
16

try - =SUM(IF(FREQUENCY(MATCH(COLUMNRANGE,COLUMNRANGE,0),MATCH(COLUMNRANGE,COLUMNRANGE,0))>0,1))

where COLUMNRANGE = the range where you have these values.

e.g. - =SUM(IF(FREQUENCY(MATCH(C12:C26,C12:C26,0),MATCH(C12:C26,C12:C26,0))>0,1))

Press Ctrl+Shift+Enter to make the formula an array (won't calculate correctly otherwise)

3
  • 1
    For some reason, this didn't work for me. My regional settings mandated I should use ';' as an argument separator and '.' as a decimal separator, but it still wasn't working. I'm using Excel 2013...maybe the API has changed?
    – ravemir
    May 19, 2014 at 14:51
  • Works for me. I'm using Excel 2013. I also didn't need to make the formula an array for it to work.
    – Mike
    Mar 14, 2016 at 22:06
  • I just tried about half a dozen different answers found in various places, and this is the only one that would count unique text values Jan 10 at 17:02
11

Here’s another quickie way to get the unique value count, as well as to get the unique values. Copy the column you care about into another worksheet, then select the entire column. Click on Data -> Remove Duplicates -> OK. This removes all duplicated values.

2
  • 1
    This one wins the "method most likely to be used by normal people" award. :) Nov 3, 2015 at 10:04
  • This is much faster than barry houdini's answer for me, with approx. 1 million rows.
    – StockB
    Feb 3, 2016 at 15:20
10

Here's an elegant array formula (which I found here http://www.excel-easy.com/examples/count-unique-values.html) that does the trick nicely:

Type

=SUM(1/COUNTIF(List,List))

and confirm with CTRL-SHIFT-ENTER

3
  • 1
    This is the most elegant I've seen, but like all the others does not do exact string matching. Apr 29, 2014 at 10:34
  • 1
    I love the twisted thinking underlying this formula.
    – MattClarke
    Feb 6, 2015 at 0:24
  • 1
    Works great on short lists. Chokes on long ones. I've got a 42000 row list that's been calculating for over five minutes and not done yet. 200 rows, imperceptible lag. 42000, waiting, waiting, waiting... Probably time to move it over to a database or some statistical app.
    – adamek
    Jul 10, 2015 at 14:23
3

Count unique with a condition. Col A is ID and using condition ID=32, Col B is Name and we are trying to count the unique names for a particular ID

=SUMPRODUCT((B2:B12<>"")*(A2:A12=32)/COUNTIF(B2:B12,B2:B12))
2

Another tricky way that just occurred to me (tested and it worked!).

  • Select the data in the column
  • In the menu, select Conditional Formatting, Highlight Cells, Duplicate Values
  • Select whether you want to highlight unique or duplicate values.
  • Save the highlight
  • Select the data
  • Go to Data and then Filter

Filter based on color:

Excel -- 2013 at least -- lets you filter on color. Sweet!

Admittedly, this is more for one-off checks of data than a spreadsheet you'll use often, since it requires some formatting changes.

1
  • Nicely efficient for a quick count! Note that you want to filter on cells with No fill.
    – cpilko
    Mar 15, 2019 at 19:07
2

With the Dynamic Array formulas(as of this posting only available to Office 365 Insiders):

=COUNTA(UNIQUE(A:A))
1

You can do the following steps:

  1. First isolate the column (by inserting a blank column before and/or after the column you want to count the unique values if there are any adjacent columns;

  2. Then select the whole column, go to 'Data' > 'Advanced Filter' and check the checkbox 'Unique records only'. This will hide all non-unique records so you can count the unique ones by selecting the whole column.

1

If using a Mac

  1. highlight column
  2. copy
  3. open terminal.app
  4. type pbpaste|sort -u|wc -l

Linux users replace pbpaste with xclip xsel or similar

Windows users, it's possible but would take some scripting... start with http://brianreiter.org/2010/09/03/copy-and-paste-with-clipboard-from-powershell/

0

You can add a new formula for unique record count

=IF(COUNTIF($A$2:A2,A2)>1,0,1)

Now you can use a pivot table and get a SUM of unique record count. This solution works best if you have two or more rows where the same value exist, but you want the pivot table to report an unique count.

0

I am using a spreadsheet with headers in row 1, data are in rows 2 and below.

IDs are in column A. To count how many different values there are I put this formula from row 2 to the end of the spreadsheet of the first available column [F in my case] : "=IF(A2=A1,F1+1,1)".

Then I use the following formula in a free cell: "=COUNTIF(F:F,1)". In this way I am sure every ID is counted.

Please note that IDs must be sorted, otherwise they will be counted more than once...but unlike array formulas it is very fast even with a 150000 rows spreadsheet.

-1

My data set is D3:D786, Column headings in D2, function in D1. Formula will ignore blank values.

=SUM(IF(FREQUENCY(IF(SUBTOTAL(3,OFFSET(D3,ROW(D3:D786)-ROW(D3),,1)),IF(D3:D786<>"",MATCH("~"&D3:D786,D3:D786&"",0))),ROW(D3:D786)-ROW(D3)+1),1))

When entering the formula, CTRL + SHIFT + ENTER

I found this at the site below, there's more explanations there about Excel that i didn't understand, if you're into that sort of thing.

http://www.mrexcel.com/forum/excel-questions/553903-count-unique-values-filtered-column.html#post2735467

I copied and pasted my dataset into a different sheet to verify it and it's worked for me.

1
  • when you don't understand something, try to comment
    – chouaib
    Nov 18, 2014 at 23:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.