Solved

Subtracting Subtotals from Subtotals

Posted on 2012-03-22
4
329 Views
Last Modified: 2012-03-28
I have the following code:

'*************************************************************
'*             Format the main Dashboard                     *
'*************************************************************
' (This routine will take 90 seconds)
    
    Dim Bcell As Range
    
Application.DisplayStatusBar = True
Application.StatusBar = "Please wait while the Dashboard is being formatted... It will take 50 sec"
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
    
    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array( _
    6 , 7 , 8, 9, 14, 15, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, _
    40, 41, 42, 43, 44, 48, 49, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64), _
 Replace:=True, _
        PageBreaks:=False, SummaryBelowData:=True
    Range("A1").Select

Open in new window


I would like to subtract

- the subtotals of columns 40 from column 6
- the subtotals of columns 41 from column 7
- the subtotals of columns 42 from column 8
- the subtotals of columns 43 from column 9
And so on...

In other words the subtotals of the second row of code from the subtotals of the first row of code

Thank you
0
Comment
Question by:fitaliano
  • 2
  • 2
4 Comments
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
Can you post some sample workbook data? that your code can be run against and then enhanced?

Dave
0
 

Author Comment

by:fitaliano
Comment Utility
This is an example of what I am trying to do.  Any suggestion to do it in a different way is welcome.
Subtotals.xls
0
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
Comment Utility
Got it.  Thanks for closing out the older questions, I was wondering about that ;)

Code creates the grouping with subtotals, then a loop to add a SUMIF to each of the subtotals based on the build of the address to that point.  At the bottom, the build is based on the entire list.

Option Explicit

Sub doSubTotals()
Dim wkb As Workbook
Dim wks As Worksheet
Dim lastRow As Long
Dim r As Range
Dim rBuild As Range
Dim rng As Range

    Set wkb = ThisWorkbook
    Set wks = wkb.ActiveSheet
    
    lastRow = wks.Range("B" & wks.Rows.Count).End(xlUp).Row
    Set rng = wks.Range("B7:E" & lastRow)
    
    rng.Subtotal groupby:=1, Function:=xlSum, TotalList:=Array(3), _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=True
        
    lastRow = wks.Range("B" & wks.Rows.Count).End(xlUp).Row
    Set rng = wks.Range("B7:E" & lastRow)
    
    For Each r In wks.Range(rng.Columns(1).Address)
        If InStr(r.Value, "Total") <> 0 Then 'found subtotal row
            If Not rBuild Is Nothing Then
                r.Offset(, 2).Formula = r.Offset(, 2).Formula & "- SUMIF(" & rBuild.Offset(, 3).Address & ",""Y""," & rBuild.Offset(, 2).Address & ")"
            Else
                Set rBuild = rng.Columns(1)
                r.Offset(, 2).Formula = r.Offset(, 2).Formula & "- SUMIF(" & rBuild.Offset(, 3).Address & ",""Y""," & rBuild.Offset(, 2).Address & ")"
            End If
            Set rBuild = Nothing
        ElseIf rBuild Is Nothing Then
            Set rBuild = r
        Else
            Set rBuild = Union(rBuild, r)
        End If
    Next r
End Sub

Open in new window


See attached.

Dave
Subtotals-r1.xls
0
 

Author Closing Comment

by:fitaliano
Comment Utility
Thank you Dave,

It worked great!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now