Adding SubTotals

AbhishekJain

New Member
Joined
Dec 30, 2016
Messages
1
Hi!!

I am looking for a code which will add subtotals in column M,N,O, Q,R,S and W,X,Y.

First I am trying to sort the transactions in column Z..

'Sort as per Type of transaction
Dim Wb1 As Workbook
Dim ws1 As Worksheet



Range("A1:AF20000").Sort _
Key1:=Range("Z1"), Header:=xlYes



' Adding subtotals
Sub ApplySubTotals()
Set ws1 = ThisWorkbook.Sheets("Purchase")
ws1.Range(ws1.UsedRange.Address).Select
Range("Z1").Activate
Selection.Subtotal GroupBy:=26, Function:=xlSum, TotalList:=Array(13, 14, _
15, 17, 18, 19, 23, 24, 25), Replace:=True, PageBreaks:=False, SummaryBelowData _
:=True
End Sub

The code does not seem to work always.. Any suggestions to improve my codes?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Welcome to the Board

Code:
Sub Abhis()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add ws.[z1], xlSortOnValues, xlAscending, , xlSortNormal
With ws.Sort
    .SetRange Range("Z1:Z" & ws.Range("z" & Rows.count).End(xlUp).Row)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlStroke
    .Apply
End With
ws.[a1].Activate                ' reference cell at column A
Selection.Subtotal 1, xlSum, Array(13, 14, 15, 17, 18, 19, 23, 24, 25), True, False, xlSummaryBelow
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,267
Members
448,558
Latest member
aivin

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