arrays count unique items?

zrx1200

Well-known Member
Joined
Apr 14, 2010
Messages
622
Office Version
  1. 2019
Platform
  1. Windows
Hello folks,

I have an array of say (s,e,se,sw,s,n,ne,ne,s,e,se) and would like the count of the unique items in that array using VBA. 7 in this example I believe.

Is this possible and how would it be done?
 
... offering what I see as a more direct method without the need for that extra 'object' whether it be a collection or a dictionary.
However, what I saw as "more direct" is relatively slow. This 'string manipulation' method is considerably faster ...

Code:
Sub CountUnique_v2()
  Dim myArray As Variant
  Dim Joined As String
 
  myArray = Array("s", "e", "se", "sw", "s", "n", "ne", "ne", "s", "e", "se")
  Joined = "|" & Join(myArray, "||") & "|"
  Do Until Replace(Joined, ".", "") = ""
    Joined = Replace(Joined, Left(Joined, InStr(2, Joined, "|")), "") & "."
  Loop
  MsgBox "Unique count = " & Len(Joined)
End Sub

... however, if speed was an issue, I think I would go for the 'collection' code of post 8. :)
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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