How do you Count the number of unique values in a list ?

G

Guest

Guest
I would like to know how many unique values I have in a given list / range.

Is there a formula to return this number. The data is generally alphanumeric.

Thanks
Sean
 
Hi, further to this thread from some time ago, I have an additional requirement of a formula based on the one issued by Aladin Akyurek =IF(LEN(A1:A6),SUMPRODUCT(1/COUNTIF(A1:A6,A1:A6)))

I want to know how many times, any values in a range are repeated a certain number of times. I'm going round in circles trying to solve this.

For example, in a column of cells containing say 1,1,1,1,2,2,2,2,3,3,3,3,4,4,5,5,6,6,6 then the formula above will return 6, as there are 6 unique values. What I want to do is know how many any values appear say 4 times (the reply would be 3, as 1,2 and 3 all appear 4 times). I realise a pivot table would be the easiest way of achieving this, but I really would like to accomplish it via formula instead.

Many thanks in advance!
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi, further to this thread from some time ago, I have an additional requirement of a formula based on the one issued by Aladin Akyurek =IF(LEN(A1:A6),SUMPRODUCT(1/COUNTIF(A1:A6,A1:A6)))

I want to know how many times, any values in a range are repeated a certain number of times. I'm going round in circles trying to solve this.

For example, in a column of cells containing say 1,1,1,1,2,2,2,2,3,3,3,3,4,4,5,5,6,6,6 then the formula above will return 6, as there are 6 unique values. What I want to do is know how many any values appear say 4 times (the reply would be 3, as 1,2 and 3 all appear 4 times). I realise a pivot table would be the easiest way of achieving this, but I really would like to accomplish it via formula instead.

Many thanks in advance!

Let A2:A20 house the example that you mention and C2 4, the occurrence frequency that you require...

D2, control+shift+enter, not just enter:

=SUM(IF(FREQUENCY(IF(A2:A20<>"",MATCH("~"&A2:A20,A2:A20&"",0)),ROW(A2:A20)-ROW(A2)+1)=C2,1))
 
Upvote 0
Hi,

Is there a way to get this to work on non-contiguous cells (ie cell ranges that are not next to each other?)

Thanks!

Bolo
 
Upvote 0
Hi,

Is there a way to get this to work on non-contiguous cells (ie cell ranges that are not next to each other?)

Thanks!

Bolo

Hard to do. Do these cells/ranges have some regularity in lay-out, which could possibly be exploited?
 
Upvote 0
Hard to do. Do these cells/ranges have some regularity in lay-out, which could possibly be exploited?

They do, but in the end i added a routine to make the ranges into one list first. But i was just wondering if it was possible using just one formula. I guess not! Thanks for your help.
 
Upvote 0
You can also use the following formula to obtain the desired count of uniques items:

=IF(LEN(A1:A6),SUMPRODUCT(1/COUNTIF(A1:A6,A1:A6)))

Hi Aladin,

I know this thread is quite old, but i was wondering. What is the point of the if len part? THe formula seems to work ok without it in excel 2007. Is the if len part something needed to trapped an error in earlier versions of excel?
 
Upvote 0
Hi Aladin,

I know this thread is quite old, but i was wondering. What is the point of the if len part? THe formula seems to work ok without it in excel 2007. Is the if len part something needed to trapped an error in earlier versions of excel?

If any cell in A1:A6 is empty or house a formula blank:

SUMPRODUCT(1/COUNTIF(A1:A6,A1:A6))

SUM(1/COUNTIF(A1:A6,A1:A6)) [ Original formula; Requires control+shift+enter ]

both would yield a #DIV/0!

To circumvent that issue and not to count an empty cell or a blank as a distinct item, you'd need...

With the SumProduct version:

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

With the Sum Version:

SUM(IF(LEN(A1:A6),1/COUNTIF(A1:A6,A1:A6))) [ Requires control+shift+enter ]

of which the shorter version is:

SUM(IF(A1:A6<>"",1/COUNTIF(A1:A6,A1:A6)))

Here are some links with explanations...

http://www.mrexcel.com/forum/showthread.php?t=36118

http://www.mrexcel.com/forum/showthread.php?t=16682

http://www.mrexcel.com/forum/showthread.php?t=70835

Also, regarding efficiency issues...

http://www.mrexcel.com/forum/showthread.php?t=292473
 
Upvote 0
so the original formula was missing the outside sum!!! that is what confused me!

Thanks for the explanations Aladin.
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,669
Members
448,977
Latest member
moonlight6

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top