I'm working on a macro where I can copy and paste values from one sheet (monthly) into another sheet (Profit&Loss). The monthly sheet will always look the same except sometime new items gets added. I already made it work but in limited way. Currently my macro takes one or several items from the monthly sheet adds them together and inserts them into my Profit&Loss sheet based on a 1-3 lookup functions. For instance, the item "Utilities" is a collection of "7430 - Utilities", "7480 - Refuse Collection" and "7540 - Telephone".
In the future I want to add more items to this collection without changing the VBA
This is what my macro currently looks like
I appreciate any help I can get
In the future I want to add more items to this collection without changing the VBA
This is what my macro currently looks like
Code:
Sub GetMonthlyData()
On Error Resume Next
Dim Tot_Row As Long
Dim Tot_Clm As Long
Table1 = Sheets("Profit&Loss").Range("A8:A59")
Table2 = Sheets("Monthly").Range("A7:G83")
Table3 = Sheets("Profit&Loss").Range("B8:B59")
Table4 = Sheets("Profit&Loss").Range("C8:C59")
Tot_Row = Sheets("Profit&Loss").Range("E8").Row
Tot_Clm = Sheets("Profit&Loss").Range("E8").Column
For Each c1 In Table1
If IsEmpty(Cells(Tot_Row, 1)) = False Then
Sheets("Profit&Loss").Cells(Tot_Row, Tot_Clm) = Application.WorksheetFunction.VLookup(c1, Table2, 7, False)
End If
If IsEmpty(Cells(Tot_Row, 2)) = False Then
Sheets("Profit&Loss").Cells(Tot_Row, Tot_Clm) = Application.WorksheetFunction.VLookup(Cells(Tot_Row, 2), Table2, 7, False) + Application.WorksheetFunction.VLookup(c1, Table2, 7, False)
End If
If IsEmpty(Cells(Tot_Row, 3)) = False Then
Sheets("Profit&Loss").Cells(Tot_Row, Tot_Clm) = Application.WorksheetFunction.VLookup(Cells(Tot_Row, 3), Table2, 7, False) + Application.WorksheetFunction.VLookup(Cells(Tot_Row, 2), Table2, 7, False) + Application.WorksheetFunction.VLookup(c1, Table2, 7, False)
End If
Tot_Row = Tot_Row + 1
Next
End Sub
I appreciate any help I can get