VBA Sort By 2 Columns On Certain Worksheets Within A Workbook

slivesay

Board Regular
Joined
Jan 4, 2019
Messages
64
I created the following code to sort one sheet by 2 columns, but I would like to change the code to include other sheets (but not all the sheets w/in the workbook). How can I do that? All the data will be in the same columns and cells on each sheet. Also, can I still use the sheet names or should the code be changed to the sheet numbers?


Sub SortColumn()

Range("B11:M123").Select
ActiveWindow.SmallScroll Down:=-102
ActiveWorkbook.Worksheets("Break").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Break").Sort.SortFields.Add Key:=Range("K11:K123") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Break").Sort.SortFields.Add Key:=Range("C11:C123") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Break").Sort
.SetRange Range("B11:M123")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
If there are not too many sheets and you wanted to hard code the names of the sheets into the code you could use something like this:

Code:
Sub SortColumn()


    Dim shtnam
    Dim i As Long


    shtnam = Array("Break", "Sheet1", "Sheet2") [COLOR=#ff0000]'Hard code your sheet names here

[/COLOR]
    For i = 0 To UBound(shtnam)
        ActiveWorkbook.Worksheets(shtnam(i)).Sort.SortFields.Clear
        ActiveWorkbook.Worksheets(shtnam(i)).Sort.SortFields.Add Key:=Range("K11:K123") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets(shtnam(i)).Sort.SortFields.Add Key:=Range("C11:C123") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets(shtnam(i)).Sort
            .SetRange Range("B11:M123")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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