The following assumes that row 1 has the column headers: "Item" in A1 and "Quantity" in B1. It will sort according to "Item" and give you the subtotals and grand total for "Quantity". You may have to change the headers and sheet name to suit your needs.
Sub SubTotal()
Dim bottomA As Integer
bottomA = Range("A" & Rows.Count).End(xlUp).Row
Range("A1:B" & bottomA).Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A" & bottomA) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A2:B" & bottomA)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.SubTotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End Sub
Bookmarks