Identifier under cursor is not recognized- count unique

vivi218

New Member
Joined
Jul 15, 2019
Messages
1
Hi dear forum users,
I am trying to find and motify a useful code to count unique row in the excel, here is the code:

#
Public Function CountUnique(rng As Range) As Long
Dim dict As Scripting.Dictionary
Dim cell As Range
Set dict = New Scripting.Dictionary
For Each cell In rng.Cells
If Not dict.Exists(cell.Value) Then
dict.Add cell.Value, 0
End If
Next
CountUnique = dict.count
End Function

#
However, it shows "Identifier under cursor is not recognized" for the first row of the code "Public Function CountUnique(rng As Range) As Long".

Anyone know why this happened and how to solve this? Thank you in advance for any help and suggestions.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to the forum.

It might be due to the difference between early binding and late binding. With early binding, like your code, you have to have the external reference to the scripting dictionary already included. With late binding, you can define it within your macro. Try this:

Code:
Public Function CountUnique(rng As Range) As Long
Dim dict As Object
Dim cell As Range

    Set dict = CreateObject("Scripting.Dictionary")
    For Each cell In rng.Cells
        dict(cell.Value) = 1
    Next
    CountUnique = dict.Count
End Function
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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