Having to Lose Focus from Main worksheet sheet To Execute Code on second worksheet to avoid error

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,538
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Consider this code:
Code:
Sub file_listing()
     With Worksheets("DYNAMIC")
                .Activate
                .Range("A2:A" & i + 1).Sort key1:=Range("A2"), Order1:=xlDescending, Header:=xlGuess
     End With
End Sub

This code is executed with a button push on sheet1 of the workbook. I'd prefer to keep sheet1 in focus while this code is being executed. The only way I can run this code without running into an error ("The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By box isn't the same or blank") , is to add the ".activate". But when I do that, the focus leaves sheet1 to go to worksheet "Dynamic". I don't want this.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try this.
Code:
[COLOR=darkblue]Sub[/COLOR] file_listing()
     [COLOR=darkblue]With[/COLOR] Worksheets("DYNAMIC")
        [SIZE=3][B][COLOR=#ff0000].[/COLOR][/B][/SIZE]Range("A2:A" & i + 1).Sort key1:=[SIZE=3][B][COLOR=#ff0000].[/COLOR][/B][/SIZE]Range("A2"), Order1:=xlDescending, Header:=xlGuess
     [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Make sure there is a period in front of both ranges.
.Range("A2:A" & i + 1)
.Range("A2")


Or this.
Code:
Sub file_listing()
    [B]Worksheets("DYNAMIC")[/B].Range("A2:A" & i + 1).Sort Key1:=[B]Worksheets("DYNAMIC")[/B].Range("A2"), Order1:=xlDescending, Header:=xlGuess
End Sub
 
Last edited:
Upvote 0
Awesome. Thank Alpha. Something as small as a "." has so much influence on success of a code.
 
Upvote 0

Forum statistics

Threads
1,203,219
Messages
6,054,216
Members
444,711
Latest member
Stupid Idiot

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