VBA - Two sort Criterias

ExcelMercy

Board Regular
Joined
Aug 11, 2014
Messages
151
Hey all,


I need to sort first by column B in Ascending order, and then by column C in Ascending order.


I used the record a macro and went through custom sort... It works. Issue is, the range will not always be the same. I was curious if it would be possible to apply the sort through all of the fields from A:GK regardless how many fields?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hey all,


I need to sort first by column B in Ascending order, and then by column C in Ascending order.


I used the record a macro and went through custom sort... It works. Issue is, the range will not always be the same. I was curious if it would be possible to apply the sort through all of the fields from A:GK regardless how many fields?
Provided there are no empty columns between A:GK, you can sort on B and C and all columns will be affected. Is that what you mean by "sort through all of the fields"?
 
Upvote 0
I mean, when I do this:

Code:
    ActiveWorkbook.Worksheets("Main_Sheet").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Main_Sheet").Sort.SortFields.Add Key:=Range( _
        "D2:D2904"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Combined OBF").Sort.SortFields.Add Key:=Range( _
        "I2:I2904"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Combined OBF").Sort
        .SetRange Range("A1:GK2904")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

It won't always be 2904 entries. Plus the recorded macro always makes things slow :/
 
Upvote 0
Here's an example

Not sure why you have the names of two different worksheets in this one sort macro???

This macro sorts on the activesheet

Change the part in red to whatever column you want to use to establish the last row

Code:
Sub SortData()
Dim LR As Long
  LR = Range("[COLOR="#FF0000"]A[/COLOR]" & Rows.Count).End(xlUp).Row
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("D2:D" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
        .SortFields.Add Key:=Range("I2:I" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
        .SetRange Range("A1:GK" & LR)
        .Header = xlYes
        .Orientation = xlTopToBottom
        .Apply
    End With
End Sub
 
Upvote 0
You are very welcome. We are happy to help.
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

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