I'm trying to figure out excel VBA code to add subtotal (=SUBTOTAL(9,__:_) formulas to multiple Total Rows within a worksheet. The issue is that the range for the subtotals can be any height. Per the image below, the hope is that the VBA code would be able to identify when "Total" is present in column B and then proceed to add a subtotal formula in that row for columns E to J. The subtotal formula would need to pull the above balances up until there is a blank cell in order to calculate the amount for the particular client (the length of the range is signified by the arrows in the image below). The range can litterally be any number of rows so has to be fully variable. Any help in this would be greatly appreciated!!
Asked
Active
Viewed 3,559 times
1
-
Why subtotal and not sum? Are you planning to hide rows you don't want in the total? – Jul 14 '18 at 15:25
-
I'd like to use subtotal bc I'd also like to add a line at the very bottom of the list that calculates the total for all of the clients. Subtotal would allow to just take the entire range up and calculate the entire current balance. Does that makes sense? – User22 Jul 14 '18 at 16:51
-
I guess I should have included that in my question :) Thank you so much for the help on this!! – User22 Jul 14 '18 at 16:53
-
What about a Pivot Table based on your data? – AJD Jul 14 '18 at 21:19
1 Answers
1
This can be accomplished with a simple SUBTOTAL across a dynamic range. Locating Total in column B can be looped.
Option Explicit
Sub totalAllClients()
Dim rng As Range, rngsb As Range, addr As String
With Worksheets("sheet11")
With Intersect(.Columns(2), .UsedRange)
Set rng = .Find(What:="total", After:=.Cells(1), MatchCase:=False, _
LookAt:=xlWhole, SearchDirection:=xlPrevious)
If Not rng Is Nothing Then
addr = rng.Address(0, 0)
Do
rng.Offset(0, 1).FormulaR1C1 = _
"=index(c2, match(""zzz"", r1c2:r[-1]c2))"
rng.Offset(0, 3).Resize(, 6).FormulaR1C1 = _
"=subtotal(109, r[-1]c:index(c, match(""zzz"", r1c2:r[-1]c2)))"
Set rng = .FindNext(After:=rng)
Loop Until rng.Address(0, 0) = addr
rng.Offset(2, 3).Resize(1, 6).FormulaR1C1 = _
"=aggregate(9, 3, r2c:r" & rng.Row & "c)"
End If
End With
End With
End Sub
-
My thought was to use subtotal bc I'd also like to add a line at the very bottom of the list that calculates the total for all of the clients. Subtotal would allow to just take the entire range up and calculate the entire current balance. Does that makes sense? – User22 Jul 14 '18 at 16:52
-
I guess I should have included that in my question :) Thank you so much for the help on this!! – User22 Jul 14 '18 at 16:53
-
-
Ok, the size of the data (number of rows) is going to change each time I'm needing to run the calculation. So would the addition you have mentioned just insert a line after the last client total? I was hoping to add this Overall Total to the macro so I don't have to add in a new line every time. Thanks again – User22 Jul 14 '18 at 17:07
-
By the way, the code you provided to add in the sum functions works great! Do you think it would be best to make these subtotals and then add code to add a subtotal line at the very bottom (last line +2) to subtotal the entire column? – User22 Jul 14 '18 at 17:24
-
-
Ok, I'm a bit of a novice vba user so not really sure how to make these changes. But I can mess with it to see if I can get it to work. Thanks – User22 Jul 14 '18 at 17:35
-
-
-
There is one additional thing that would allow me to do everything through the macro. Any way to add to the above code a way to transfer the client name to the cell just to the right of the corresponding Total. So as an example, I'd be looking to copy client name "AAAA" in cell B2 over to cell C4. And copy "BBBB" in B5 to cell C13....copy "CCCC" in B14 to cell C16....and so forth. Again, the file is going to change sizes so it would have to use the looping for Total and then be able to recognize where the corresponding client name is... then copy that cell to the right of the Total. – User22 Jul 14 '18 at 18:41
-
Hello, I updated the picture for this question to show an actual spreadsheet I'm trying to use the subtotal macro for. The macro isn't working for some reason. Any idea of what the problem might be? Thanks – User22 Jul 16 '18 at 20:50
-
Hello, I have been trying to use to code as stated above but it isn't calculating any of the subtotals at this point. Thanks for any help you can provide on this! – User22 Jul 17 '18 at 23:20