count unique

jebenexcel

Board Regular
Joined
Mar 16, 2018
Messages
59
Hi,
I found a user defined function that counts unique entries in a range. Why does it output 0 for me?
Code:
Function COUNTUNIQUE(DataRange As Range, CountBlanks As Boolean) As Integer
Dim CellContent As Variant
Dim UniqueValues As New Collection
Application.Volatile
On Error Resume Next
For Each CellContent In DataRange
If CountBlanks = True Or IsEmpty(CellContent) = False Then
UniqueValues.Add CellContent, CStr(CellContent)
End If
Next
COUNTUNIQUE = UniqueValues.Count
End Function
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I have tested the function and it works correctly

Do this to satisfy yourself that it works:
- create a NEW workbook
- place some values (including blanks) in A1 to A20
- paste the UDF code into a standard module in that workbook
- put this formula in cell B1
=countunique(A1:A20,TRUE)

If it still is not working, then let me know
 
Upvote 0
Is there a limit on how many entries it can count?

It counts fine if there's 20, 200, 2000 entries, but when i try to use it on my 200k entry sample it returns a 0.
 
Upvote 0
It would have been helpful if you had mentioned that "extra" detail earlier :)

I have tested again and the function tops out at 32767 unique values
- there appears to be an upper a limit on the number of "keys" in a collection

I cannot immediately think of a quick fix for you
- the trick in the function (which makes it efficient) is that duplicate values are rejected from being added to the collection by using the fact that collection "keys" cannot be duplicated.
 
Last edited:
Upvote 0
Make this change
Code:
Function COUNTUNIQUE(DataRange As Range, CountBlanks As Boolean) As [COLOR=#ff0000]Long[/COLOR]
 
Upvote 0
@Fluff -well spotted :)
@jebenexcel - here is a different way
You need to add a reference to Mircrosoft Scripting Runtime
(In VBA window click Tools \ References \ scroll down to Mircrosoft Sripting Runtime\ check the box \ OK)

Code:
Function CountUnique2(Rng As Range) As Long
    Dim dict As Dictionary, cell As Range
    Set dict = New Dictionary
    For Each cell In Rng.Cells
        If Not dict.Exists(cell.Value) Then dict.Add cell.Value, 0
    Next
    CountUnique2 = dict.Count
End Function
 
Last edited:
Upvote 0
Or without needing a reference
Code:
Function CountUnique2(Rng As Range) As Long
    Dim dict As Object, cell As Range
    Set dict = CreateObject("scripting.dictionary")
    For Each cell In Rng.Cells
        If Not dict.Exists(cell.Value) Then dict.Add cell.Value, 0
    Next
    CountUnique2 = dict.count
End Function
 
Upvote 0
@Fluff
- that does avoid adding reference
- but is 20% slower (on my PC)

Whilst testing that, I also tested the original function - that was the fastest of all! :laugh:
 
Upvote 0

Forum statistics

Threads
1,224,798
Messages
6,181,038
Members
453,014
Latest member
Chris258

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