Home

Sorts, page breaks and subtotals, oh my...

Strider051
New Contributor

Hello to anyone reading this! I'm having difficulty writing a macro and was hoping one of you would be generous enough to help me out.

 

What I'm trying to do:

 

I am trying to import data to a templated excel sheet and execute a macro that will sort by two columns (A = Item description, H = Tag type), Page break after a change in column H and if there is no change (not all item descriptions have a tag type entry) a break after Column A, and finally a sub total for every break on column H and a Total for every break on column A (using sums from Columns B and O). If possible I'd like to auto adjust for column width and wrap. Please not that the ranges are dynamic, however the column headers will remain in the same order.

 

Any help is appreciated!!!

 

What I have so far:

 

Sub SubTotals()

    ActiveSheet.Copy After:=Worksheets(Worksheets.Count)

    Worksheets(Worksheets.Count).Name = "Totals"

    Worksheets("Totals").Activate

   

    Dim arow As Long

    Dim hrow As Long

   

    arow = Cells.Find(What:="*", _

                    After:=Range("A1"), _

                    LookAt:=xlPart, _

                    LookIn:=xlFormulas, _

                    SearchOrder:=xlByRows, _

                    SearchDirection:=xlPrevious, _

                    MatchCase:=False).Row

                   

    hrow = Cells.Find(What:="*", _

                    After:=Range("H1"), _

                    LookAt:=xlPart, _

                    LookIn:=xlFormulas, _

                    SearchOrder:=xlByRows, _

                    SearchDirection:=xlPrevious, _

                    MatchCase:=False).Row

                   

    Range("A2").Select

    ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Clear

    ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Add Key:=Range("A2:A" & CStr(arow)) _

        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Add Key:=Range("H2:H" & CStr(hrow)) _

        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

    With ActiveWorkbook.Worksheets("Totals").Sort

        .SetRange Range("A1:O" & CStr(arow))

        .Header = xlYes

        .MatchCase = False

        .Orientation = xlTopToBottom

        .SortMethod = xlPinYin

        .Apply

    End With

    Selection.Subtotal GroupBy:=8, Function:=xlSum, TotalList:=Array(2, 15), _

        Replace:=False, PageBreaks:=True, SummaryBelowData:=True

    Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2, 15), _

        Replace:=False, PageBreaks:=False, SummaryBelowData:=True

   

End Sub

 

This is not functioning as intended as it seems to not total B,O on the page break for column A changes. Ex. column A has the values Plant 1, Plant 2, Plant 3. Column H has values A,B,(Blank). The blanks in H seem to gum up the works and cause the pages to no longer break on changes in A. There are bound to be other issues, I know almost nothing about VBA.

 

Example file included. (no macros included in file)

1 Reply

I could for sure use a hand on this still if anyone has the inclination I would appreciate it!

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
21 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
13 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies