Solved

Subtotals Multiple Excel Sheets

Posted on 2014-04-04
15
1,044 Views
Last Modified: 2014-04-06
I would like to use VBA to Subtotal Multiple Excel Sheets. For Each change in the Project Name column I want to use the sum function and add the subtotal to the Balance Amount. Not all sheets in the Workbook will be subtotaled. Thanks
0
Comment
Question by:shieldsco
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 9

Expert Comment

by:nick2253
Comment Utility
Using this snippet will let you select data in other sheets.

ThisWorkbook.Sheets("name of sheet 2").Range("A1")

Open in new window

Depending on how the data is laid out in the sheet, you may need to change that from a single cell to a range, and then process on that range.
0
 
LVL 12

Expert Comment

by:Harry Lee
Comment Utility
Can you upload a sample file?
0
 
LVL 8

Expert Comment

by:itjockey
Comment Utility
See attached
Sum-all-sheet.xlsx
0
 
LVL 8

Expert Comment

by:itjockey
Comment Utility
it is simple formula based which SUM the all sheet Cell K2 which is between sheet "Start - Finish".so if any new sheet you insert between this two sheets sum total change accordingly.

thanks
0
 

Author Comment

by:shieldsco
Comment Utility
Harry - find attached sample. I only want to subtotal sheets with Emp Name
Test.xlsm
0
 

Author Comment

by:shieldsco
Comment Utility
Itjockey - it not sum all sheets - it's put subtotals on all sheets
0
 
LVL 8

Expert Comment

by:itjockey
Comment Utility
Emp? or EPA?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:shieldsco
Comment Utility
Itjockey - I want to subtotal each sheet that has a Employee Name - see sample
0
 
LVL 12

Accepted Solution

by:
Harry Lee earned 500 total points
Comment Utility
shieldsco,

I have came up with this VBA. Please test. Currently, instead of replacing the Balance column, I have added an extra column between the Invoice Amount and Balance Amount called Project Total. I can change it if you don't like it.

I had also put as much comments as I can come up with in the code to make it easy for you to understand what the code is doing.

Sub Subtotaling()
Dim WS As Worksheet, RWs As Long, CLMNs As Long, StartAddr As String, EndAddr As String, I As Long

'Work with sheets only if name is not equal to Summary or Invoice
For Each WS In Worksheets
    If WS.Name <> "Summary" And WS.Name <> "Invoice" Then
        
'Add new balance column if new balance column does not exist
        If WS.Range("G1") <> "Project Total" Then
            WS.Range("G:G").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            WS.Range("G1") = "Project Total"
        Else
            WS.Range("G2:G" & Cells(Rows.Count, 1).End(xlUp).Row).Clear
        End If

    RWs = WS.Cells(Rows.Count, 1).End(xlUp).Row
'Correct Number stored as text
        Dim I2 As Object
        For Each I2 In WS.Range("J2:J" & RWs)
            I2 = I2.Value
        Next I2

'Sort all columns first by Project # then by Date
    CLMNs = WS.Cells(1, Columns.Count).End(xlToLeft).Column
    WS.Sort.SortFields.Clear
    WS.Sort.SortFields.Add Key:=Range("J:J"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    WS.Sort.SortFields.Add Key:=Range("B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With WS.Sort
        .SetRange Range(Cells(1, 1), Cells(RWs, CLMNs))
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

'Apply Subtotal to New

    For I = 2 To RWs
        If WS.Cells(I, 10) <> WS.Cells(I - 1, 10) Then
            StartAddr = WS.Cells(I, 6).Address
        End If
        
        If WS.Cells(I, 10) <> WS.Cells(I + 1, 10) Then
            EndAddr = WS.Cells(I, 6).Address
            WS.Cells(I, 7).Formula = "=sum(" & StartAddr & ":" & EndAddr & ")"
        End If
    Next

'Auto fit column widths
        Range("A1").Select
        WS.Columns.AutoFit
    End If
Next
End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:Harry Lee
Comment Utility
Forgot to mention to you. Your original macro has the hot-key set to Ctrl-A.

I would highly recommend you to change the hot-key to something else. Ctrl-A is default to Select All in Excel. If you use Ctrl-A as your macro hot-key, you loose the Select All default hot-key.

Also, once you are happy with my VBA, I would highly suggest you to combine my VBA with your Invoice macro by copying my code starting from the Dim all the way to the line right before End Sub, and paste it to the back of the Invoice macro. This way, you don't have 2 steps. By doing it all at once, you will avoid errors by changing where the columns are.
0
 

Author Closing Comment

by:shieldsco
Comment Utility
Harry Thanks - see my code below

Sub SubTotals()
    Dim LastRow As Long
    Dim wsDst As Worksheet
     
    For Each wsDst In ThisWorkbook.Sheets
        If wsDst.Name <> "Invoice" And wsDst.Name <> "Summary" Then
            With wsDst
                LastRow = .Range("A" & Rows.Count).End(xlUp).Row
                .Range("A1:T" & LastRow).Subtotal GroupBy:=10, Function:=xlSum, TotalList:=Array(7), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
            End With
        End If
    Next
     
End Sub
0
 
LVL 12

Expert Comment

by:Harry Lee
Comment Utility
That works too. What you are doing is to use the internal subtotal function, which will create whole brunch of + and - buttons on the Row number area.

Also, it's inserting extra subtotal rows on the spreadsheet. In my opinion, those extra rows make it extremely hard to read the spreadsheet.

Opps! I think I misunderstood your question. You want to sub the Balance column instead of the Invoice Amount column.

Let me know if you want me to change the vba so that it sums the Balance Column as well.
0
 
LVL 12

Expert Comment

by:Harry Lee
Comment Utility
Regardless which way you want to go,

I think you need to make some modification to your VBA. Currently, there is error in there due to the sorting of the Project # or Project Name.

Look at the Karla Keppner tab, and Project# 112368. It's being broken down to multiple pieces and there are more than 1 subtotal for the Project.

Refer to my VBA for the Fixing Number stored as Text section, and the Sorting section. You can incorporate those 2 sections to your vba to fix such issue.
0
 

Author Comment

by:shieldsco
Comment Utility
Harry - I'm also using data connections with Access so I sort before the data reaches Excel
0
 

Author Comment

by:shieldsco
Comment Utility
Look at the Karla Keppner tab, and Project# 112368. It's being broken down to multiple pieces and there are more than 1 subtotal for the Project.

Harry - I'm sorting on Project Name not Project#
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

728 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

14 Experts available now in Live!

Get 1:1 Help Now