Sum Levels of Nested Subtotals

Mayanwolfe

New Member
Joined
Jun 5, 2013
Messages
27
Hello,

I am trying to write a macro that sums several layers of nested subtotals. The lowest level subtotals (subtotal 2) are already calculated and pulled from another tab. Subtotal 1 needs to ONLY be the sum of the individual Subtotal 2 lines. In my example below, the first subtotal 2 field should be equal to 1350. Grand total should only be the sum of the Subtotal 1 lines. The challenge is that all of the individual amounts and subtotals are all within the same column. So far, I've tried to sum by picking out the subtotal lines by color. I've indicated the colors of the various lines in parenthesis below. However, in the real version, there are even more nested subtotals, and most of them are the same color grey. Also, I'm not sure how to get it to stop when it reaches the prior group of subtotals that have already been summed.

It would seem that I need to loop outward from the value column, scanning down until "total" text is identified, then sum the values of the appropriate color above that line, but stop when the prior total line is reached. After this is complete for the range, the loop would continue outward until the Grand Total. However, this is above my current skillset. Does anyone have any ideas on how to proceed? Thanks in advance for any thoughts.

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Grand Total[/TD]
[TD]Subtotal 1[/TD]
[TD]Subtotal 2[/TD]
[TD]Values[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]150[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Total (grey)[/TD]
[TD]250 (grey)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]500[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]600[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Total (grey)[/TD]
[TD]1100 (grey)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Total (brown)[/TD]
[TD](brown)[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]50[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]30[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Total (grey)[/TD]
[TD]80 (grey)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Total (grey)[/TD]
[TD]100 (grey)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Total (brown)[/TD]
[TD](brown)[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]150[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]150[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD]Total (grey)[/TD]
[TD]300 (grey)[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Total (brown)[/TD]
[TD](brown)[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Grand Total (dk. grey)[/TD]
[TD](dk. grey)[/TD]
[TD](dk. grey)[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
































I've found a function that I've been trying to modify to make this work, but I'm not sure how to make the range dynamic enough to exclude groups of values that have been previously summed.

Option Explicit
Function SumByColor(DefinedColorRange As Range, SumRange As Range)

Application.Volatile

'Declaring variables
Dim ICol As Integer
Dim GCell As Range

'Getting the interior color of the cell
ICol = DefinedColorRange.Interior.ColorIndex

'Looping through the defined range
For Each GCell In SumRange

If ICol = GCell.Interior.ColorIndex Then
'Getting the sum of matching colored cell
SumByColor = SumByColor + GCell.Value
End If
Next GCell

End Function
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

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