The Computer Oracle

How to count number of distinct values in a range?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 How To Count Number Of Distinct Values In A Range?
00:37 Accepted Answer Score 40
00:53 Answer 2 Score 8
01:00 Answer 3 Score 3
02:41 Answer 4 Score 2
03:26 Answer 5 Score 1
03:45 Thank you

--

Full question
https://superuser.com/questions/189762/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#microsoftexcel #worksheetfunction

#avk47



ACCEPTED ANSWER

Score 40


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

will do it without having to use an array formula.




ANSWER 2

Score 8


I found a solution here which seems to be an incredible roundabout way to solve it. But hey, it works...

=SUM(IF(COUNTIF(A2:A100,A2:A100)=0, "", 1/COUNTIF(A2:A100,A2:A100)))

and then press Ctrl+Shift+Enter. Pressing only Enter will give the wrong result.




ANSWER 3

Score 3


This article shows this for text values:

=SUM(IF(FREQUENCY(IF(LEN(C3:C25)>0,MATCH(C3:C25,C3:C25,0),""), IF(LEN(C3:C25)>0,MATCH(C3:C25,C3:C25,0),""))>0,1))

and this for numeric values:

=SUM(IF(FREQUENCY(C3:C25, C3:C25)>0,1))

This article shows similar formulas, but also shows a method using filters.

Count the number of unique values by using a filter

You can use the Advanced Filter to extract the unique values from a column of data and paste them to a new location. Then you can use the ROWS function to count the number of items in the new range.

  1. Ensure that the first row in the column has a column header.
  2. On the Data menu, point to Filter, and then click Advanced Filter.
  3. In the Advanced Filter dialog box, click Copy to another location.
  4. If the range that you are counting is not already selected, delete any information in the List range box and then click the column (or select the range) that contains your data.
  5. In the Copy to box, delete any information in the box or click in the box, and then click a blank column where you want to copy the unique values.
  6. Select the Unique records only check box, and click OK.

    The unique values from the selected range are copied to the new column.

  7. In the blank cell below the last cell in the range, enter the ROWS function. Use the range of unique values that you just copied as the argument. For example, if the range of unique values is B1:B45, then enter:
    =ROWS(B1:B45)




ANSWER 4

Score 2


=SUM(1/COUNTIF(A2:A100;A2:A100))

Confirm with Ctrl+Shift+Enter

For each cell, it counts how many times it occurs, and summes the inverses of all these values. Suppose some string or number occus 5 times. Its inverse is 0.2 which get summed 5 times, so 1 is added. In the end it gives the number of different values.

Note: doesn't work when blanks occur!




ANSWER 5

Score 1


Found two resources for you:

http://www.excelforum.com/excel-worksheet-functions/365877-count-distinct-values.html

and

http://www.cpearson.com/excel/Duplicates.aspx

You should be able to find a workable solution from there.