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
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: