Link to home
Create AccountLog in
Avatar of neoptoent
neoptoentFlag for United States of America

asked on

excel get count of distinct names

I am trying to find an excel formula to get the count of distinct names in column A based on the number in column b

so in this table, I would want the distinct number of names in column A where the value in column B is 2 (so in this example the count would be 3... jane, bob and mary)

I cant figure out this formula

Please help
The list is dynamic so I need to use the whole column for ranges
Avatar of Amit Kumar
Amit Kumar
Flag of India image

Use pivot table, put names in filter and team in value. You will get results.
Avatar of Flyster
If you want to use a formula, you can try this:


This is an array formula, so you enter it into your cell select ctrl+shift+enter.

Use this formula:


Change the bottom row to include the maximum number of possible rows.

Enter the formula by pressing CTRL+SHIFT+ENTER.

The formula handles blank rows so you don't need to worry about the bottom row in the cell references. Just make it low enough to handle all possible scenarios.

Avatar of Professor J
Professor J

simple like this without use of control shift enter


Open in new window

Avatar of neoptoent


I have the excel sheet as a named table
Would I be able to use the table name as a reference instead of a range

Table name: cname

Just click on the b2:b9 then erase it with back space then with mouse select the column of the table then the table name with its column will be automatically put inside formula. You don't have to type the table name and column name there. If you have to then put the table name then [ column name ]

If I had a thirds column and wanted to do the counting based on the value 2 in column b as well as the value of "yellow" in column c

So count all distinction values in column A where column b is 2 and column c is yellow

Then it is not possible only with formula then it requires merging UDF with formula
Not sure what that means... Can I use a formula ?
that means that inside formula you could use the UDF.

see attached file.  it now works with condition of yellow cells as well.

the below code is inserted in module of attached workbook which create the defined function within the formula


Open in new window

as you can see the new argument criteria if GetCellColor of the range equals to 65535 which means yellow then true otherwise false.  

remember the formula is array formula and requires the special keystroke of Control Shift and Enter to enter.

Function GetCellColor(xlRange As Range)
Dim indRow, indColumn As Long
Dim arResults()
If xlRange Is Nothing Then
Set xlRange = Application.ThisCell
End If
If xlRange.Count > 1 Then
ReDim arResults(1 To xlRange.Rows.Count, 1 To xlRange.Columns.Count)
For indRow = 1 To xlRange.Rows.Count
For indColumn = 1 To xlRange.Columns.Count
arResults(indRow, indColumn) = xlRange(indRow, indColumn).Interior.Color
GetCellColor = arResults
GetCellColor = xlRange.Interior.Color
End If
End Function

Open in new window

Just to make sure. The actual cell color won't change, the word yellow will appear in the cell
In this case it is even easier. No need for any UDF.

Assuming the column to check the value yellow is range c2:c10 then =SUM((B2:B10=2)*(A2:A10<>"")*(C2:C10="yellow")/COUNTIFS(A2:A10,A2:A10,B2:B10,B2:B10,C2:C10,C2:C10))
And this will give me the unique values in a
I am getting a #div/0 error
please see attached.
In a sheet I am working with the a2:a10 <> "" is equal to 0
So that is causing the error

How can I fix that
ok change this a2:a10 <> "" to a2:a10 <> "0"

if it does not work, can u please upload a example?
ok the issue is when the column has blanks in the rows

If there a way to ignore the empty cells
see attached example.
if column b or c have a blank cell then I get the same eror of div0.
Is there a way to ignore blanks in column B and C.
Add this inside that formula right next to (A2:A10<>"")

Like this (A2:A10<>"")*(C2:C10<>"")*(B2:B10<>"")
Y'all seem to be having a lot of fun with this.

I'll repeat my formula above as it had already solved the original question and, with the addition of two more conditions ("0" in column A and "Yellow" in column C), still solves the problem with any number of data rows:


Enter the formula by pressing CTRL+SHIFT+ENTER.


yes, really had fun :-)

without control shift enter ;-)


with attachment

if you delete the contents of c10 so the cell is empty the formula shows a div/0
can if ignore the empty cells in columns b &C?

please find attached.  this version requires Control + Shift + Enter and it will take care of the empty cells too.
Can you please copy the formula into the thread, can't open the file
Avatar of Professor J
Professor J

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account