Multi level sorting where header is in the 3rd row

exce101

New Member
Joined
Jan 27, 2018
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have dynamic data in my worksheet (named "Setup" currently but could change) which I am looking to apply a multi-level sort to.

The current dataset starts from A4 and ends at AD37 however this could also change as more data in rows and columns is added. The headers are in the third row while the first two rows are blank to house the macro buttons.

Below is the code I am currently using but need to tweak it to specify that the headers are in the second row rather than the first row.

Any help is greatly appreciated.

Sub MultiLevelSort()

Worksheets("Setup").Sort.SortFields.Clear
'Worksheets("Setup").UsedRange.Sort Key1:=Range("b3"), Key2:=Range("C3"), Key2:=Range("D3"), Header:=xlYes, _
'Order1:=xlAscending, Order2:=xlAscending, Order3:=xlDescending


End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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’)

specify that the headers are in the second row
You said third row earlier & in your thread title, so I will assume third.

I would be a little nervous about relying on UsedRange when there are (currently) blank rows at the top. My suggestion would be something like this.

VBA Code:
Sub MultiLevelSort_v2()
  With Sheets("Setup")
    With Intersect(.UsedRange, .Rows("3:" & Rows.Count))
      .Sort Key1:=.Columns(2), Order1:=xlAscending, _
            Key2:=.Columns(3), Order2:=xlAscending, _
            Key3:=.Columns(4), Order3:=xlDescending, _
            Header:=xlYes
    End With
  End With
End Sub
 
Upvote 0
Solution
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 said third row earlier & in your thread title, so I will assume third.

I would be a little nervous about relying on UsedRange when there are (currently) blank rows at the top. My suggestion would be something like this.

VBA Code:
Sub MultiLevelSort_v2()
  With Sheets("Setup")
    With Intersect(.UsedRange, .Rows("3:" & Rows.Count))
      .Sort Key1:=.Columns(2), Order1:=xlAscending, _
            Key2:=.Columns(3), Order2:=xlAscending, _
            Key3:=.Columns(4), Order3:=xlDescending, _
            Header:=xlYes
    End With
  End With
End Sub
Thank you so much for the speedy response. This works perfectly well.

I suppose if I wanted to make the code worksheet name agnostic, I would replace with activesheet?

I have now also updated my Excel Version (365) and Platform (Windows)
 
Upvote 0
Upvote 0
(y) Thanks for updating (though looks like you actually missed the bold bit ;))
That’s strange … I’ve checked now, updated and saved. Hopefully it’s come through.

Thanks once again for your help.
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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