Solved

Cumulative subtotals in Excel

Posted on 2012-03-09
5
501 Views
Last Modified: 2012-03-23
I have the following code to run subtotals

    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(5, 6, 7, 8, 9, _
        13, 14, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), Replace:=True, _
        PageBreaks:=False, SummaryBelowData:=True
    Range("A1").Select

Open in new window


I want to do the cumulative sum from Column 21 to 29

So the value in column 21 should be

(subtotal 20 + subtotal 21) = New value in column 21

The value in columns 22 should be

New value in column 21 + subtotal 22

And so on....
0
Comment
Question by:fitaliano
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 37703806
There is no cumulative function in the group subtotal command, however, what happens as you probably know is when that command is executed, Excel goes in and inserts rows/columns adding the subTotal() function.

So at this point, you merely need to go through that function row/column and update it.

Do you have a brief example spreadsheet that goes with this code?  I can better/more quickly help with this conversion if you'd post one.

Thanks,

Dave
0
 

Author Comment

by:fitaliano
ID: 37703916
There you go Dave, what I am trying to do is the progressive cumulative Cash Flow from 2006 to 2015.
Subtotal-Example.xls
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37703921
That's from U to AC?  I don't speak column numbers, so just confirming...

Dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37703956
I think that's right.

Here's your code:
Option Explicit

Sub cumlSubtotals()
Dim wkb As Workbook
Dim wks As Worksheet
Dim lastRow As Long
Dim r As Range
Dim rRow As Range
Dim rng As Range

    Set wkb = ThisWorkbook
    Set wks = wkb.Sheets("Dashboard")
    
    lastRow = wks.Range("A" & wks.Rows.Count).End(xlUp).Row
    
    Set rng = wks.Range(wks.Range("U4:AC4"), wks.Range("U" & lastRow, "AC" & lastRow))
    
    For Each rRow In rng.Rows
        For Each r In Range(rRow.Address)
            If Not r.Formula Like "=SUBTOTAL*" Then 'assume entire row to find has the subtotals, so save time skipping out if not
                Exit For
            Else 'found the subtotal row
                r.Formula = "=" & r.Offset(, -1).Address & " + " & Right(r.Formula, Len(r.Formula) - 1)
            End If
        Next r
    Next rRow
End Sub

Open in new window


See attached demonstration workbook.

Dave
Subtotal-Example-r1.xls
0
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 37703967
Code optimized:

Sub cumlSubtotals()
Dim wkb As Workbook
Dim wks As Worksheet
Dim lastRow As Long
Dim r As Range
Dim rRow As Range
Dim rng As Range

    Set wkb = ThisWorkbook
    Set wks = wkb.Sheets("Dashboard")
    
    lastRow = wks.Range("A" & wks.Rows.Count).End(xlUp).Row
    
    Set rng = wks.Range(wks.Range("U4:AC4"), wks.Range("U" & lastRow, "AC" & lastRow))
    
    For Each rRow In rng.Rows
        Set r = rRow.Cells(1, 1)
        If r.Formula Like "=SUBTOTAL*" Then 'assume entire row to find has the subtotal in first col
            rRow.Formula = "=" & Replace(r.Offset(, -1).Address, "$", "") & " + " & Right(r.Formula, Len(r.Formula) - 1)
        End If
    Next rRow
End Sub

Open in new window


See attached.

Cheers,

Dave
Subtotal-Example-r3.xls
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question