Sorting Data within Subtotalled Groups

beanie3105

New Member
Joined
Dec 21, 2016
Messages
18
Hello

I have about 34k lines worth of data that is subtotalled, I need to sort the data within these subgroups, I can't sort the whole data as it puts the subtotals to the end, is there a way of doing this?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

You have given us very little to go on in terms of your data layout, what the subtotals are based on, what column(s) needs to be sorted etc but here is an small example of one way to approach the problem

Here is my sample data with the 'Value' column for each 'Age' group subtotalled.

beanie3105.xlsm
ABC
1NameAgeValue
2Name 1105
3Name 11105
4Name 3102
5Name 7105
610 Total17
7Name 2116
8Name 4115
911 Total11
10Name 10129
11Name 5126
12Name 9127
1312 Total22
14Name 8136
1513 Total6
16Name 6144
1714 Total4
18Grand Total60
Sheet1
Cell Formulas
RangeFormula
C6C6=SUBTOTAL(9,C2:C5)
C9C9=SUBTOTAL(9,C7:C8)
C13C13=SUBTOTAL(9,C10:C12)
C15,C17C15=SUBTOTAL(9,C14:C14)
C18C18=SUBTOTAL(9,C2:C16)


Now I want each of those subtotalled groups sorted by 'Value' highest to lowest.

VBA Code:
Sub SortInSubtotals()
  Dim rA As Range
  
  Application.ScreenUpdating = False
  For Each rA In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
    rA.EntireRow.Sort Key1:=rA.Columns(3), Order1:=xlDescending, Header:=xlNo
  Next rA
  Application.ScreenUpdating = True
End Sub

Result:

beanie3105.xlsm
ABC
1NameAgeValue
2Name 1105
3Name 11105
4Name 7105
5Name 3102
610 Total17
7Name 2116
8Name 4115
911 Total11
10Name 10129
11Name 9127
12Name 5126
1312 Total22
14Name 8136
1513 Total6
16Name 6144
1714 Total4
18Grand Total60
Sheet1
Cell Formulas
RangeFormula
C6C6=SUBTOTAL(9,C2:C5)
C9C9=SUBTOTAL(9,C7:C8)
C13C13=SUBTOTAL(9,C10:C12)
C15,C17C15=SUBTOTAL(9,C14:C14)
C18C18=SUBTOTAL(9,C2:C16)
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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