[VBA] Count number of occurances in an Array?

Safety

New Member
Joined
Feb 2, 2011
Messages
17
Hey guys,

I have a variable length array in VBA whose number of elements will change with usage. For example:
Code:
[ Apple, Orange, Banana, Banana, Apple, Banana ]
I would like to count the number of occurances of each element within the array and then return both the element and its count in a msgbox. For example

Code:
MsgBox

Apple x 2
Orange x 1
Banana x 3
I've searched the forums but could not find a similar solution.

Any advice would be greatly appreciated!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Thanks for the suggestion Peter but I don't believe Worksheet functions work with an array.

I think the solution involves a combination of loops with counters but I'm having trouble with that part of the code. For example:

Code:
For i = LBound(MyArray) To UBound(MyArray)
For k = LBound(MyArray) To UBound(MyArray)
If MyArray(i) = MyArray(k) Then
counter = counter + 1
End If
NameString(i) = MyArray(i) + counter
Next k
Next i
The second part of the problem is figuring out how to removie the duplicates from the MsgBox. For Example:
Code:
MsgBox

Apple x 2
Orange x 1
Banana x 3
Banana x 3
Apple x 2
Banana x 3
 
Upvote 0
Maybe something like this

Code:
Sub aTest()
    Dim myArray() As Variant, dict As Object
    Dim i As Long, v As Variant
    
    myArray = Array("Apple", "Orange", "Banana", "Banana", "Apple", "Banana")
    Set dict = CreateObject("Scripting.Dictionary")
    
    For i = LBound(myArray) To UBound(myArray)
        If dict.exists(myArray(i)) Then
            dict.Item(myArray(i)) = dict.Item(myArray(i)) + 1
        Else
            dict.Add myArray(i), 1
        End If
    Next i
    
    For Each v In dict.keys
        MsgBox v & " " & dict.Item(v)
    Next v
End Sub

M.
 
Upvote 0
It works perfectly Marcelo! Using the array elements as keys for a dictionary and then using that dictionary object to remove the duplicates was brilliant (!!!). Thank you :cool:
 
Upvote 0
It works perfectly Marcelo! Using the array elements as keys for a dictionary and then using that dictionary object to remove the duplicates was brilliant (!!!). Thank you :cool:

You are welcome and thanks for the feedback.

M.
 
Upvote 0
You are welcome and thanks for the feedback.

M.

Hi marcelo

Is there any chance you can explain how the dict scripting and exits and jeys method works.

Hopefully you can help understand how this works.

Awesome code
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,819
Members
449,469
Latest member
Kingwi11y

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