VBA Multi-level Sorting

default_name

Board Regular
Joined
May 16, 2018
Messages
170
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
I have seen several examples of multi-level sorting online, but I am struggling to find a method that works best for my needs.
I am trying to sort all of the data on sheet1 (the data spans from columns A to R).

Here is what I currently have (including the order in which I am trying to sort the data on):
VBA Code:
Sheets("sheet1").Range("A1:R" & Sheets("sheet1").Range("A1").End(xlDown).Row).Sort _
key1:=Sheets("sheet1").Range("A:A"), order1:=xlAscending, _
key2:=Sheets("sheet1").Range("G:G"), order2:=xlAscending, _
key3:=Sheets("sheet1").Range("F:F"), order3:=xlAscending, _
key4:=Sheets("sheet1").Range("B:B"), order4:=xlAscending, _
key5:=Sheets("sheet1").Range("M:M"), order5:=xlAscending, _
Header:=xlYes

I get an error when I attempt this. I have stepped through it and am realizing that it only works with 3 keys. I need it to work with 5.
I tried simply copying the code again with my remaining 2 columns, but that didnt work out so well. I need to sort all of the data together.

How can I make this work?
Thanks in advance
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You cannot use that style of sort for more than three Keys. You will need to use the "new" style of sort.
You can use the macro recorder to get the basis of the code.

The other option is to sort on cols G & H and then sort again for cols A, B & F.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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