a macro button for sorting and subtotaling rows of data...(# of rows unknown)

rmarenyi

New Member
Joined
Nov 12, 2017
Messages
3
Hi,
I was previously given some code that would take a list and sort it by check # and then subtotal amounts by check # with a grand total at the end. The code was associated with a 'button' on the page to click whenever you wanted to create the summation page.
When I was given the code I must not of specified that the rows are not static (although the columns are) so when I ran this for a large number of items it appears to only sort the first 9 or so lines and then commence to subtotal by check #.

I need the code to sort the complete list (minus the row of column titles) given that the length of the list is unknown.

Here's what the page looks like where A1 through G1 are the column headings. Data begins with A2 and goes through G2. The row count are infinite and different each time.

LOTDescriptionQTYClient SharePAIDDATE PAID CHECK #<img width="136" height="42" alt="Create Client Summary">

<tbody>
</tbody>
xmirror ornate40.00 y6/19/18 1169
xgold mirror oval15.00 y4/30/18 1129
xround table gold grim50.00 y6/19/18 1169
xstork62.50 y6/19/18 1169
xgold mirrored candle holders250.00 y4/30/18 1129
xcaned vanity chair25.00 y6/19/18 1169
xdemilune table100.00 y4/30/18 1129
xNM mirrors355.00 y6/19/18 1169
xdrop down small desk62.50 y4/30/18 1129
xRECORD PLAYER50.00 y4/30/18 1129
xside table w/glass25.00 y6/19/18 1169
xsunflower oil painting50.00 y6/19/18 1169
xKJV bible37.50 y6/19/18 1169
xTK Beside Still Waters32.50 y6/19/18 1169
xTK Light of Peace75.00 y4/30/18 1129
xcalligraphy michael podesta20.60 y6/19/18 1169
xSunflowers on canvas30.00 y4/30/18 1129
xoil paintings whimsical instruments2100.00 y4/30/18 1129
xcoral silk french chairs2225.00 y6/19/18 1169
xice cream chairs2150.00 y6/19/18 1169
xred velvet vanity chair40.00 y6/19/18 1169
xgold chair pink seat17.50 y7/20/18 1186
xgold marble pedestal table52.92 y7/20/18 1186
cherry side table60.00 y6/19/18 1169
xvelvet stools225.00 y7/20/18 1186
xside table tile top50.00 y6/19/18 1169
xchild rolltop desk32.50 y7/20/18 1186

<colgroup><col width="69" style="width: 69pt;"><col width="69" style="width: 69pt;"><col width="69" style="width: 69pt;"><col width="69" style="width: 69pt;"><col width="69" style="width: 69pt;"><col width="69" style="width: 69pt;"><col width="69" style="width: 69pt;"><col width="69" span="4" style="width: 69pt;"></colgroup><tbody>
</tbody>

Here is the Code I was provided with for the worksheet CLIENT LOG. The macro is SUBTOTAL2

Code:
Sub SUBTOTAL2()'
' SUBTOTAL2 Macro
'


'
    ActiveWindow.SmallScroll ToRight:=-16
    ActiveWindow.SmallScroll Down:=1
    ActiveWindow.SmallScroll ToRight:=-1
    ActiveWindow.SmallScroll Down:=5
    ActiveWindow.SmallScroll ToRight:=-1
    ActiveWindow.SmallScroll Down:=1
    ActiveWindow.SmallScroll ToRight:=1
    ActiveWindow.SmallScroll Down:=-199
    Columns("F:F").Select
    ActiveWorkbook.Worksheets("CLIENT LOG").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("CLIENT LOG").Sort.SortFields.Add Key:=Range( _
        "G2:G11"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("CLIENT LOG").Sort
        .SetRange Range("A1:G11")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Cells.Select
    Range("B1").Activate
    Selection.SUBTOTAL GroupBy:=6, Function:=xlSum, TotalList:=Array(4), _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End Sub

Like I said, it works great if I presort the page but the code was supposed to do that and seems to only sort the first 9 lines of data.....
Thanks in advance for the help.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Does this do what you want...

Code:
Sub SubTotal()


    Dim wsCL As Worksheet: Set wsCL = Worksheets("Client Log")
    Dim lRow As Long
    
    lRow = wsCL.Cells(Rows.Count, 2).End(xlUp).Row
    ActiveWorkbook.Worksheets("CLIENT LOG").Sort.SortFields.Clear
    Range("A1:G" & lRow).Select
    wsCL.Sort.SortFields.Clear
    wsCL.Sort.SortFields.Add Key:=Range("G2:G" & lRow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With wsCL.Sort
        .SetRange Range("A1:G" & lRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    wsCL.Range("A1").SubTotal GroupBy:=7, Function:=xlSum, TotalList:=Array(4), _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    Range("A1").Select
    
End Sub
 
Upvote 0
does this do what you want...

Code:
sub subtotal()


    dim wscl as worksheet: Set wscl = worksheets("client log")
    dim lrow as long
    
    lrow = wscl.cells(rows.count, 2).end(xlup).row
    activeworkbook.worksheets("client log").sort.sortfields.clear
    range("a1:g" & lrow).select
    wscl.sort.sortfields.clear
    wscl.sort.sortfields.add key:=range("g2:g" & lrow) _
        , sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal
    with wscl.sort
        .setrange range("a1:g" & lrow)
        .header = xlyes
        .matchcase = false
        .orientation = xltoptobottom
        .sortmethod = xlpinyin
        .apply
    end with
    wscl.range("a1").subtotal groupby:=7, function:=xlsum, totallist:=array(4), _
        replace:=true, pagebreaks:=false, summarybelowdata:=true
    range("a1").select
    
end sub

fabulously!!! Thank you!!
 
Upvote 0
You're welcome. Thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,215,590
Messages
6,125,701
Members
449,250
Latest member
azur3

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