Sorting by one column, grouping by another

Red over White

Board Regular
Joined
Jul 16, 2011
Messages
123
Office Version
  1. 365
Platform
  1. MacOS
Following a search of other threads, I tried the following program:

Dim Last As Long
Last = Range("A" & Rows.Count).End(xlUp).Row

Sheets("Current").Select
With Sheets("Current").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("E5:E" & Last), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("B5:B" & Last), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange Range("A5:K" & Last)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub

To produce

Column BColumn E
Apples60000
Apples53000
Apples22
Pears 55000
Oranges54000
Oranges16000

<colgroup><col span="2"></colgroup><tbody>
</tbody>

The idea being the highest number in column E appears at the top, followed by any subsequent number that shares the same product name in Column B

Any ideas how I produce this?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi,

I think you just need to swap these:
Code:
        .SortFields.Add Key:=Range("B5:B" & Last), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("E5:E" & Last), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
You need to sort all the like values in Column B together then sort column E values within those.


regards,
 
Upvote 0
Hi RickXL (and anyone else)

I swopped the code around as suggested but it didn't have the desired effect. What I am trying to do is interrogate column E to find the largest number in that column, and that figure goes in E5. Then it looks across to column B and sees what fruit is there (in this case apples), and then lists any row that has apples in column B sorted in descending order based on column E. It then looks for the next highest number in column E and puts that under the 'apple' rows, and repeats the process until all fruits have been similarly sorted.

Thanks
 
Upvote 0
All the Pears at the top in column B, followed by oranges and apples. The numbers in column E were simply the order they appear in row order.
 
Upvote 0
Sorry, I did not notice that column B was in the wrong order. try this:
Code:
        .SortFields.Add Key:=Range("B5:B" & Last), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("E5:E" & Last), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

regards,
 
Upvote 0
RickXL

This just produces the reverse, namely:

BE
2Pears55000
5Oranges16000
3Oranges54000
6Apples 22
4Apples 53000
1Apples 60000

<colgroup><col span="3"></colgroup><tbody>
</tbody>

In column A I have put the order which they first appeared, and the current view shows how they have ended up. I'm pretty sure that I have to have
.SortFields.Add Key:=Range("E5:E" & Last), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
at the top as I want to get the highest value first. The program then needs to put all the same values in column B together and then put them in descending order based on column E. Once it has done those, it needs to stop, find the next highest value in column E and go through the same process. That's the bit I am struggling with.
 
Upvote 0
No, you need to sort by the names in column B first. If you do not do that then they will not be grouped together.

Here is my test code:
Code:
Sub mySort()
    Dim Last As Long
    
    Last = Range("B" & Rows.Count).End(xlUp).Row
    With Sheets("Current").Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("B5:B" & Last), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SortFields.Add Key:=Range("E5:E" & Last), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .SetRange Range("A5:K" & Last)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
and here is my test data. Note, the =RAND() in column A is so that I can "mess up" the data before re-testing the code.

Excel 2013
ABCDE
4Column AColumn BColumn E
50.557421815Apples60000
60.523562447Apples53000
70.591662488Apples22
80.533648141Oranges54000
90.41809225Oranges16000
Current
Cell Formulas
RangeFormula
A5=RAND()



Regards,
 
Upvote 0
RickXL

I put your code in and thought it had worked but then realised Pears was at the bottom, you haven't included this entry in your set
 
Upvote 0
Pears is at the bottom because P for Pears comes after O for Orange.

I just added it to my test data to check it for you.

Is that not what you wanted?
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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