Excel formula: count the number of different values
Solved
smer38
Posted messages
13
Status
Membre
-
nono -
nono -
Hello,
I am trying to count the number of unique values in a column, but I can't seem to do it. Does anyone have an idea?
Thank you.
I am trying to count the number of unique values in a column, but I can't seem to do it. Does anyone have an idea?
Thank you.
4 réponses
My comment on 'g's reply from December 3, 2012, at 3:14 PM was deleted with the justification that "The message has been deemed obsolete." However, this conversation does not ultimately clarify what the best solution is! Therefore, I will give my opinion/summary:
The best solution is the formula
[A] {
It provides accurate counts of distinct values in the cell range for all types of tested values: strings from the alphabet, numbers, and non-alphanumeric characters (+, /, ...), as well as error values (#DIV/0!, #N/A, ...) and logical values (TRUE, FALSE), and even the empty cell value (which is not counted as a distinct value).
Be careful, it can be long to calculate (9 seconds for 50 cells, 15 seconds for 500, 25 seconds for 5000, ... error for 50000)
The second formula suggested, by ccm81, [B] {
To avoid it returning an error value (#DIV/0!) as soon as a cell is empty, it is best to replace empty cells with a specific value, e.g., 'empty' (or a dot, or even a space but be careful of less distinguishable omissions). The number of distinct values found then does include this "empty" value, which may be desired at times. The same remark applies regarding the calculation length.
Finally, the formula first mentioned by g [C] {
The best solution is the formula
[A] {
=SUM(IF(A1:A10<>"",1/COUNTIF(A1:A10,A1:A10)))} (the last one proposed in the topic, by tongtong)
It provides accurate counts of distinct values in the cell range for all types of tested values: strings from the alphabet, numbers, and non-alphanumeric characters (+, /, ...), as well as error values (#DIV/0!, #N/A, ...) and logical values (TRUE, FALSE), and even the empty cell value (which is not counted as a distinct value).
Be careful, it can be long to calculate (9 seconds for 50 cells, 15 seconds for 500, 25 seconds for 5000, ... error for 50000)
The second formula suggested, by ccm81, [B] {
=SUMPRODUCT(1/COUNTIF(A1:A7,A1:A7))} also works very well, provided there are no empty cells in the search range, or that we specifically want to count this empty cell as a distinct value:
To avoid it returning an error value (#DIV/0!) as soon as a cell is empty, it is best to replace empty cells with a specific value, e.g., 'empty' (or a dot, or even a space but be careful of less distinguishable omissions). The number of distinct values found then does include this "empty" value, which may be desired at times. The same remark applies regarding the calculation length.
Finally, the formula first mentioned by g [C] {
=SUM(IF(COUNTIF(A1:A7,A1:A7)=1,1))} should be forgotten: it gives inaccurate values as soon as a value is duplicated... One just has to see its count evolve strangely when counting a range of cells containing a single character (it finds 1), then that character twice (it finds 0), then three times (0), ...
The origin of the formula comes from English-speaking forums, as Laurent Longre mentioned 10 or 15 years ago:
And thank you for this highly documented history.