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:

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,156
Messages
5,600,040
Members
414,357
Latest member
Gemma_R

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
Top