This should give the basic idea – Use the item you want to count as the key, and the count itself as the value. If it is already in the Dictionary, increment it. If not, add it:
Const TEST_DATA = "Apple,Orange,Banana,Banana,Orange,Pear"
Sub Example()
Dim counter As New Scripting.Dictionary
Dim testData() As String
testData = Split(TEST_DATA, ",")
Dim i As Long
For i = LBound(testData) To UBound(testData)
If Not counter.Exists(testData(i)) Then
counter.Add testData(i), 1
Else
counter.Item(testData(i)) = counter.Item(testData(i)) + 1
End If
Next
Dim key As Variant
For Each key In counter.Keys
Debug.Print key, counter(key)
Next
End Sub
4
solved VBA Dictionary for uniqueness count