help needed to subtotal a sheet with variable rows from month to month

inolab

New Member
Joined
Sep 28, 2016
Messages
16
I have a report that is ran once a month and each sheet in the report has a variable number of rows each month. I have used the macro recorder to subtotal each sheet, but on sheets with more rows, the code does not include them in subtotal. I have tried using an offset but that didn't work either. Can someone help me determine what I can change so that the selected data will go to the last row with data before subtotaling?

Code:
   Range("A1:P6").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range("A1:P364").Select
    ActiveSheet.PageSetup.PrintArea = "$A$1:$P$364"
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .PrintTitleRows = "$1:$1"
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = "$A$1:$P$364"
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
[COLOR=#800080]       (I deleted page setup details to make this code shorter and it works fine)   [/COLOR]
    End With
    Application.PrintCommunication = True
    Selection.Subtotal GroupBy:=6, Function:=xlCount, TotalList:=Array(7), _
        Replace:=True, PageBreaks:=True, SummaryBelowData:=True
    Range("F1").Select
    Selection.End(xlDown).Select

Thank you for your help.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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