• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1541
  • Last Modified:

Subtotals Multiple Excel Sheets

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
shieldsco
Asked:
shieldsco
  • 6
  • 5
  • 3
  • +1
1 Solution
 
nick2253Commented:
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
 
Harry LeeCommented:
Can you upload a sample file?
0
 
Naresh PatelTraderCommented:
See attached
Sum-all-sheet.xlsx
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Naresh PatelTraderCommented:
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
 
shieldscoAuthor Commented:
Harry - find attached sample. I only want to subtotal sheets with Emp Name
Test.xlsm
0
 
shieldscoAuthor Commented:
Itjockey - it not sum all sheets - it's put subtotals on all sheets
0
 
Naresh PatelTraderCommented:
Emp? or EPA?
0
 
shieldscoAuthor Commented:
Itjockey - I want to subtotal each sheet that has a Employee Name - see sample
0
 
Harry LeeCommented:
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
 
Harry LeeCommented:
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
 
shieldscoAuthor Commented:
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
 
Harry LeeCommented:
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
 
Harry LeeCommented:
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
 
shieldscoAuthor Commented:
Harry - I'm also using data connections with Access so I sort before the data reaches Excel
0
 
shieldscoAuthor Commented:
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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now