I'm trying to create a trend analysis that shows the total spent for each account code. I want to sum the total for each account code, by year, and place the information on another worksheet, called "Results". Included in that information I would like the heading titles for the sum of that account code listed as well. The data will come from the worksheet called "DATA". Below I've included screenshots mock worksheets and of the vba code I tried.
I've tried to use a for loop that I found on another site, but I'm not sure if I'm headed down the right path or if it is even possible to do what I'm asking. I know the code isn't correct. I used their example as a guide. Any guidance would be greatly appreciate!
"DATA" Worksheet
"RESULTS" Worksheet - Empty
"RESULTS" Worksheet - How I want it to look after the vba code but of course with data input into every field. All of the information for every field can be found in the "DATA" worksheet.
The Results of my VBA Code
I've tried to use a for loop that I found on another site, but I'm not sure if I'm headed down the right path or if it is even possible to do what I'm asking. I know the code isn't correct. I used their example as a guide. Any guidance would be greatly appreciate!
"DATA" Worksheet
"RESULTS" Worksheet - Empty
"RESULTS" Worksheet - How I want it to look after the vba code but of course with data input into every field. All of the information for every field can be found in the "DATA" worksheet.
VBA Code:
' IMPORTANT: Add the Dictionary reference to use the dictionary
' Tools->Reference and check "Microsoft Scripting Runtime"
' https://excelmacromastery.com/
Sub ForDictionary_Assign_Sum()
' PART 1 - Read the data
' Get the worksheets
Dim shRead As Worksheet
Set shRead = ThisWorkbook.Worksheets("DATA")
' Get the range
Dim rg As Range
Set rg = shRead.Range("A1").CurrentRegion
' Create the dictionary
Dim dict As New Dictionary
' Read through the data
Dim i As Long, SpeedType As String, MonetaryAmount As Long, Account As Long
For i = 2 To rg.Rows.Count
' Store the values in a variable
SpeedType = rg.Cells(i, 1).Value2
MonetaryAmount = rg.Cells(i, 68).Value2
Account = rg.Cells(i, 8).Value2
' The item will be automatically added if it doesn't exist
dict(SpeedType) = dict(SpeedType) + Account + MonetaryAmount
Next i
' PART 2 - Write the data
Dim shWrite As Worksheet
Set shWrite = ThisWorkbook.Worksheets("Reports")
With shWrite
' Clear the data in output worksheet
.Cells.ClearContents
' Set the cell formats
'.Columns(2).NumberFormat = "$#,##0;[Red]$#,##0"
' Write header
.Cells(1, 1).Value2 = "SpeedType"
.Cells(1, 2).Value2 = "Account"
.Cells(1, 3).Value2 = "MonetaryAmount"
End With
Dim key As Variant, row As Long
row = 2
' Read through each item in the Dictionary
For Each key In dict.Keys
shWrite.Cells(row, 1) = key
shWrite.Cells(row, 2) = dict(key)
row = row + 1
Next key
End Sub
The Results of my VBA Code
