Apply subtotals to multiple sheets

ifeelalone

New Member
Joined
Mar 6, 2017
Messages
31
I am trying to loop to apply subtotals through all of my sheets except "Summary" and "Reference_list".
But I am getting an error "
Run-time error 1004 : This can't be applied to the selected range. Select a single cell in a range and try again"

Notes:
1. the table starts on A3
2. apply subtotal in Column J and Column M
3. at each change in Column A

I am using this code:

Sub SubTotals() Dim LastRow As Long
Dim wks As Worksheet

For Each wks In ActiveWorkbook.Worksheets
If wks.Name <> "Summary" And wks.Name <> "Reference_List" Then
With wks
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A3:S" & LastRow).Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(10, 13), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End With
End If
Next

End Sub


Here's a sample of a sheet:
Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
1
Title of this sheet here
2
Deposits
Amount Posted
3
Month_Name
ColumnB
ColumnC
ColumnD
ColumnE
ColumnF
ColumnG
ColumnH
ColumnI
ColumnJ
ColumnK
ColumnL
ColumnM
ColumnM
ColumnO
ColumnP
ColumnQ
ColumnR
ColumnS
4
January
01/05/16​
Alvin Empleo
1335​
1,222.00​
1,222.00
1,222.00​
1,222.00
1/12/2016​
315129462​
5
January
01/05/16​
Alvin Empleo
1335​
1,222.00​
1,222.00
1,222.00​
1,222.00
1/12/2016​
315129462​
6
January
01/05/16​
Alvin Empleo
1335​
1,222.00​
1,222.00
1,222.00​
1,222.00
1/12/2016​
315129462​

<tbody>
</tbody>
Sheet: Alvin Empleo

<tbody>
</tbody>

 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Maybe one or more sheets do not have any data? Maybe you could check for existing data before applying the subtotal. For example, you can check whether the last row in Column A is greater than Row 3...

Code:
Sub SubTotals()

    Dim LastRow As Long
    Dim wks As Worksheet
    
    For Each wks In ActiveWorkbook.Worksheets
        If wks.Name <> "Summary" And wks.Name <> "Reference_List" Then
            With wks
                LastRow = .Range("A" & [COLOR=#ff0000].[/COLOR]Rows.Count).End(xlUp).Row
                [COLOR=#ff0000]If LastRow > 3 Then[/COLOR]
                    .Range("A3:S" & LastRow).Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(10, 13), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
                [COLOR=#ff0000]End If[/COLOR]
            End With
        End If
    Next
    
End Sub

Also, note that I've qualified the reference for Rows.Count by adding a period before it.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,963
Messages
6,122,484
Members
449,088
Latest member
Melvetica

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top