So I’m trying to analysis some data in Excel and having some trouble finding the most frequent numbers. I have an unknown number of locations which can have an unknown number of donations. For example
- Brantford $50.00
- Brantford $25.00
- Brantford $50.00
- Windsor $200.00
- Quebec $25.00
- Quebec $100.00
- Quebec $50.00
- Quebec $50.00
- Quebec $25.00
- Quebec $50.00
- Quebec $50.00
- Quebec $25.00
- Quebec $100.00
- Quebec $40.00
- Windsor $140.00
- Windsor $20.00
- Windsor $20.00
So I need to use VBA to find for each location the count, sum, mean and mode (has to be done through VBA, can’t just write instructions on how to do this using advanced filters/pivot tables :().
So right now using VBA I have a dictionary object that stores the location name as a key and each donation in a collection. Using the count of the collection I have the count, can easily loop through the collection for the sum, using those I have the mean; but, I am not sure the most efficient way to get the mode.
I know I can find it if my data was in an array using Application.mode, but that doesn’t appear to work for collections :(. Converting a collection to an array though to find the mode though really doesn’t strike me as the most efficient solution. Only other option i can find of though is to sort the collections then loop through them to find the mode.
So wondering if anyone knows of a good way to find the statistical mode of a collection?
Dim locdata As Object
Set locdata = CreateObject("scripting.dictionary")
For counter = 2 To max
mykey = Cells(counter, loccol).value
If Not (locdata.exists(mykey)) Then
locdata.Add (mykey), New Collection
End If
locdata(mykey).Add (Cells(counter, donamountcol).value)
Next counter
For Each k In locdata.keys
locname = k
Cells(counter, 1) = k
Cells(counter, 2) = locdata(k).Count
donationtotal = 0
For Each donvalue In locdata(k)
donationtotal = donationtotal + donvalue
Next donvalue
Cells(counter, 3) = donationtotal
Cells(counter, 4) = donationtotal / CDbl(locdata(k).Count)
'Cells(counter, 5) = Application.mode(locdata(k)) doesn't work :(
counter = counter + 1
Next k
edit: Ideally the output should be (using Quebec as an example)
Quebec: Count: 10 Sum: 515 Average: 51.5 Mode: 50
I actually just decided to make a dictionary of dictionaries. So I have the locations and each location than has a dictionary of the count of each donation amount. Was easy enough to compare counts that way to find the mode.