greengo204
Technical User
I am trying to create a Macro that sorts data and then adds subtotals. This macro is to be used on different spreadsheets with different numbers of columns. Although the macro is to be used on different spreadsheets the first 4 columns are to always be excluded from the subtotaling so at current my code looks like:
Sample Code:
Sample data:
A/C Code A/C Name A/C Manager N/C Grand Total Fixed Trans International
xxxxxx xxxxx East 2201301 1000 500 500 0
gggggg ggggg North 2201301 3000 2000 1000 0
kkkkkk kkkkkk South 2201301 4000 1000 1000 2000
nnnnnn nnnnn West 2201301 5000 3000 2000 0
When ever I try run this macro where “TotalList:=Array” does not exactly match the number of columns in a spreadsheet I encounter the following error “subtotal method of range class failed”
My problem is that I’m trying to create a dynamic array for the “TotalList:=” so I can add subtotals to all columns to the right of column 4 (N/C) in the above case 5 to 8. In some cases the number of columns can be up to 50.
Cheers
Sample Code:
Code:
Cells.Select
Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Key2:=Range("C2") _
, Order2:=xlAscending, Key3:=Range("E2"), Order3:=xlDescending, Header _
:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
xlSortNormal
Selection.Subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, _
8), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
ActiveSheet.Outline.ShowLevels RowLevels:=2
Sample data:
A/C Code A/C Name A/C Manager N/C Grand Total Fixed Trans International
xxxxxx xxxxx East 2201301 1000 500 500 0
gggggg ggggg North 2201301 3000 2000 1000 0
kkkkkk kkkkkk South 2201301 4000 1000 1000 2000
nnnnnn nnnnn West 2201301 5000 3000 2000 0
When ever I try run this macro where “TotalList:=Array” does not exactly match the number of columns in a spreadsheet I encounter the following error “subtotal method of range class failed”
My problem is that I’m trying to create a dynamic array for the “TotalList:=” so I can add subtotals to all columns to the right of column 4 (N/C) in the above case 5 to 8. In some cases the number of columns can be up to 50.
Cheers