Sum unique categories in an array

Certified

Board Regular
Joined
Jan 24, 2012
Messages
189
I have an excel sheet with 24K rows of monetary transactions.

Each transaction is given a category; There are 6 different categories.

I stored the transactions into an array.

I want to sum each category and store them (category and total amount) into another array.

How would I go about doing that?

summary
SUM 24K transactions stored in an array by category and place the result into a new array.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If the categories are in column A, and the amounts are in column B, then try:

=SUMIF(A:A,"Housing",B:B)

If you want your other array on a different sheet, and you have the categories in A1:A6, put this formula in B1

=SUMIF(Sheet1!A:A,A1,Sheet1!B:B)

and drag it down to B6.
 
Upvote 0
Something like this could work:

Code:
Sub test3()
Dim MyArray(1000, 2), Results As Object
   
    Set Results = CreateObject("Scripting.Dictionary")
    
    For i = 1 To UBound(MyArray)
        Results(MyArray(i, 1)) = Results(MyArray(i, 1)) + MyArray(i, 2)
    Next i
    
    Range("A1").Resize(Results.Count) = WorksheetFunction.Transpose(Results.keys)
    Range("B1").Resize(Results.Count) = WorksheetFunction.Transpose(Results.items)
        
End Sub
but frankly, if your data starts on a sheet, the formulas would be better.
 
Upvote 0
This works perfectly.

Two questions:
1. How do you create a dictionary with checking to see if the key EXISTs?

2. How could I save the results in an array?

Thanks for your help!
 
Upvote 0
Try:

Code:
Sub test3()
Dim MyArray(1000, 2), Results As Object, Array2(6, 2), i As Long, x As Variant
   
  
    Set Results = CreateObject("Scripting.Dictionary")
    
    For i = 1 To UBound(MyArray)
        Results(MyArray(i, 1)) = Results(MyArray(i, 1)) + MyArray(i, 2)
    Next i
    
    If Results.exists("Housing") Then Debug.Print "It's here"
    
    i = 1
    For Each x In Results
        Array2(i, 1) = x
        Array2(i, 2) = Results(x)
        i = i + 1
    Next x
    
End Sub
A dictionary is perfect for checking to see if a key exists, since you don't need to read through the entire array to see if it's there.
 
Upvote 0
One other way to move a dictionary to an array:

Code:
    MyKeys = Results.keys
    MyItems = Results.items
Each of those lines generates a 1-dimensional 0-based array of the contents of the dictionary. But if you want to put both the keys and items in a 2-D array, you'll need the code in the last post.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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