VBA to Sort on a *different* sheet (than the one with focus)

d0rian

Active Member
Joined
May 30, 2015
Messages
313
Office Version
  1. 365
Simple answer, I'm sure, but I can't figure this out. My code is:

VBA Code:
Sub clean_sheet_sort()
   
    With Sheets("clean")
        Dim LastRow As Long
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        Range("A1:L" & LastRow).sort Key1:=Range("A1:A" & LastRow), _
            Order1:=xlAscending, Header:=xlYes
    End With

End Sub

I want to be able to run this from any sheet (and have it sort columns A:L on sheet 'Clean'.)
The problem is that it sorts cols A:L on whatever sheet I have selected/in focus.
I thought the With Sheets("clean") / End With instructed the VBA to perform the action on sheet 'clean' regardless of what sheet is in focuse...but that's obviously not happening, so what am I doing wrong?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
As explained in the With VBA help you just forgot the dots between each object reference !​
A demonstration as a beginner starter :​
VBA Code:
    With Sheets("clean").UsedRange
        .Sort .Cells(1), 1, Header:=1
    End With
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

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