Sumproduct return unique values only not working

doug firr

Board Regular
Joined
Mar 28, 2011
Messages
140
This formula is to return the unique values in a range. The range in question is a list of email addresses.

=SUMPRODUCT(1/COUNTIF($P$2:$P$70348,$P$2:$P$70348))

I have used this formula to count unique values before and have not had problems.

It returns 0 when it should be closer to 50 or 60k.

Any ideas?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Andrew - your absolutely right. I left for a meeting and came back 20 minutes later - my 0's had turned to more realistic numbers. Problem solved itself.

Control+shift+enter, not just enter:

Rich (BB code):
=SUM(IF(FREQUENCY(IF($P$2:$P$70348<>"",MATCH($P$2:$P$70348,$P$2:$P$70348,0)),
    ROW($P$2:$P$70348)-ROW($P$2)+1),1))

This should be faster.
 
Upvote 0
Maybe a query table would be preferable? SQL below. Or a pivot table.

Code:
SELECT COUNT(*) AS MyCount
FROM
(SELECT DISTINCT field_name
FROM your_data)
 
Upvote 0
This formula is to return the unique values in a range. The range in question is a list of email addresses.

=SUMPRODUCT(1/COUNTIF($P$2:$P$70348,$P$2:$P$70348))

I have used this formula to count unique values before and have not had problems.

It returns 0 when it should be closer to 50 or 60k.

Any ideas?
Try this array formula**:

=SUM(IF(FREQUENCY(MATCH($P$2:$P$70348,$P$2:$P$70348,0),ROW($P$2:$P$70348)-ROW($P$2)+1),1))

** array formulas need to be entered using the key
combination of CTRL,SHIFT,ENTER (not just ENTER).
Hold down both the CTRL key and the SHIFT key
then hit ENTER.
 
Upvote 0
Options like pivot tables or advance filter with unique value ticked or UDF or vba.
Array formulas over massive range will down slow calculation grounding halt.
 
Upvote 0
Options like pivot tables or advance filter with unique value ticked or UDF or vba.
Array formulas over massive range will down slow calculation grounding halt.
On my old gutless machine the array formula I suggested takes slightly less than 1 second to calculate. That's not too bad considering what the formula is doing.

If the OP is interested in using a faster UDF...

Code:
Public Function COUNTU(theRange As Range) As Variant
'Code by Charles Williams
    Dim colUniques As New Collection
    Dim vArr As Variant
    Dim vCell As Variant
    Dim vLcell As Variant
    Dim oRng As Range
    Set oRng = Intersect(theRange, theRange.Parent.UsedRange)
    vArr = oRng
    On Error Resume Next
    For Each vCell In vArr
    If vCell <> vLcell Then
        If Len(CStr(vCell)) > 0 Then
             colUniques.Add vCell, CStr(vCell)
        End If
    End If
    vLcell = vCell
    Next vCell
    
    COUNTU = colUniques.Count
End Function
Then:

=COUNTU($P$2:$P$70348)

This UDF is just about twice as fast to calculate compared to the array formula. On my machine it took less than half a second to calculate.
 
Upvote 0
On my old gutless machine the array formula I suggested takes slightly less than 1 second to calculate. That's not too bad considering what the formula is doing.

If the OP is interested in using a faster UDF...

...

Then:

=COUNTU($P$2:$P$70348)

This UDF is just about twice as fast to calculate compared to the array formula. On my machine it took less than half a second to calculate.

Your gutless old machine is awesome at slightly less than 1 second. My work PC takes about 4 minutes. I guess that is why I don't use array formulas - stopped using them ~10 years ago in fact.

For me the UDF calculates as for you.
 
Upvote 0
FWIW, I recall an old post where array formulas were taking over an hour and a query table solution only a fraction of a second.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,629
Members
452,661
Latest member
Nonhle

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