Step 2: VBA Macro to Sum

LearnVBA83

Board Regular
Joined
Dec 1, 2016
Messages
113
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I have a bunch of sheets in a tab that are all hardcoded. Is there a way to write VBA code to look for the word Expense in column B and add the sum functions for C - O?

Things i'm trying to avoid: Sometimes there isn't a number hardcoded which could impact the sum function in VBA. The number of rows in each category can also vary. I'm not great at VBA and formulas but it's almost like the macro would need to find expense, move one cell over, add a sum function upwards until it reaches the cell below the month, and do that for columns C-O each time you see the word expense in column B.

Just trying to replace those hardcoded expense totals with a formula. In the end i'll be trying to loop through a bunch of worksheets to do this with VBA. I'm just trying to do it in steps.

Capture.JPG
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
try this code:
VBA Code:
Sub test()
lastRow = Cells(Rows.Count, "B").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastRow, 15)) ' load all the dworksheet into memory
startrow = 2
For i = 2 To lastRow
 If inarr(i, 2) = "Expense" Then
  For j = 3 To 15
    tt = Chr(64 + j)
   Range(Cells(i, j), Cells(i, j)).Formula = "=Sum(" & tt & startrow & ":" & tt & i - 1 & ")"
    Next j
    startrow = i + 2
  End If
Next i
End Sub
 
Upvote 0
Thank you! This seemed to work well. Two things: When i look at the first word "expense" it seems the formula went all the way to the top. First image below. When i look at the rest of the "expense" it seems the formula is including the month. Is there a way to fix that piece?

1706716554038.png



1706716569298.png
 
Last edited:
Upvote 0
When i look at the first word "expense" it seems the formula went all the way to the top.
Adjust this by changing this line to start on the corrct row:
VBA Code:
startrow = 2
I failed to spot that you had pasted an image starting at row 5 so startrow should be 6 at the start.
When i look at the rest of the "expense" it seems the formula is including the month. Is there a way to fix that piece?
In your orginal picture you had two rows between the "sum" row and where the next lot of numbers start. In the second picture you have 3 rows so that . I had assumed that you always had 2 rows between each set of numbers, if it is 3 rows then you can adjust it by changing this line:
VBA Code:
startrow = i + 2
to
VBA Code:
startrow = i + 3
If the number of rows between each set of numbers varies that is something different and means more code to detect where the numbers start each time, this is possible if you need it
 
Upvote 0
If the number of rows between each set of numbers varies that is something different and means more code to detect where the numbers start each time, this is possible if you need it
I think I would want it to detect where the numbers start each time since I don't have control over the initial creating of the data set so someone could add a row in and it may not be consistent each month.

Thank you so much! It's amazing what coding can do!
 
Upvote 0
try this code which detects the start of the set of number by checking column C to see whether the value is numeric and not blank
VBA Code:
Sub test()
lastRow = Cells(Rows.Count, "B").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastRow, 15)) ' load all the dworksheet into memory
Startrow = ""
For i = 1 To lastRow
 If Startrow = "" Then
    If IsNumeric(inarr(i, 3)) And inarr(i, 3) <> "" Then
     Startrow = i
    End If
 Else
    If inarr(i, 2) = "Expense" Then
     For j = 3 To 15
       tt = Chr(64 + j)
      Range(Cells(i, j), Cells(i, j)).Formula = "=Sum(" & tt & Startrow & ":" & tt & i - 1 & ")"
     Next j
       Startrow = ""
    End If
 End If
Next i
End Sub
 
Upvote 0
Hi there! Thanks again for helping me out here. I ran the macro on a couple worksheets and they vary. I think it's b/c the sheet isn't always standard. Let me know if you want me to load a spreadsheet using the XL2BB ad in in excel. I just learned about that yesterday!

1. Expense Total

1706798270035.png



2nd Expense Total worked!

1706798304458.png


3rd Expense Total didn't input a formula

1706798337608.png


4th Expense Total went too hight

1706798435769.png


5th Expense Total worked

1706798470127.png
 
Upvote 0
The problem is because the code is looking for numbers in the column B, so if there no numbers in column B obviously it doesn't work. Is there a column which will always have numbers in it? i.e. column O because it would be easy to change that, To detect which column has numbers makes it even more complicated. ( this is what I call "Requrements creep"!!)
Also the first "sum" includes the first row because there is a number in it!! If you want the code to detect where to start you must define a logical rule that will find the start row. If you can write down a rule it can be coded, I have just been guessing what your data looks like so not surprising it doesn't always work
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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