Excel VBA Range.sort alternative?

khlau

New Member
Joined
Jan 22, 2018
Messages
13
Hello all,

Wondering if someone could help? I am trying to sort two tabs in my workbook (three total tabs) which I am able to do with the below:
Code:
Sub SortAllSheets()


Sheet1.Activate


Dim lrow As Long
lrow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1:R" & lrow).Sort key1:=Range("J1:J" & lrow), order1:=xlDescending, Header:=xlYes


Sheet3.Activate


Dim lrow2 As Long
lrow2 = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1:H" & lrow2).Sort key1:=Range("E1:E" & lrow2), order1:=xlDescending, Header:=xlYes



End Sub

However I am wondering if it would be possible to achieve this without the "Sheet1.Activate" & "Sheet3.Activate" commands? and what the corresponding code would be?

Thanks.
 
Last edited by a moderator:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Yes, you can do it like this:

Code:
Sub SortAllSheets()

    With Sheet1
        Dim lrow As Long
        lrow = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Range("A1:R" & lrow).Sort key1:=.Range("J1:J" & lrow), order1:=xlDescending, Header:=xlYes
    End With

    With Sheet3
        Dim lrow2 As Long
        lrow2 = .Cells(Rows.Count, 1).End(xlUp).Row
        .Range("A1:H" & lrow2).Sort key1:=.Range("E1:E" & lrow2), order1:=xlDescending, Header:=xlYes
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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