VBA - Remain on 'Active Page'

Peter Muller

Board Regular
Joined
Oct 15, 2018
Messages
133
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I have a macro that does multiple tasks on different sheets in my workbook.

I have the same macro button on each sheet. When I run the macro, it does what I want it to, but my cursor does not remain on the active sheet, but jumps to a different sheet.


I am looking for code to keep the cursor on the active sheet and not jump to another.

I tried various options, but none works, e.g.,

With ActiveSheet

Range("A1").Select

End With


Please help
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi Peter,

At the start of your code, store the worksheet that you want to remain on. Then perform your code and at the end, activate the stored worksheet and then append your example code to the bottom.

VBA Code:
Sub blahblah()
    Dim ws  As Worksheet
    
    Set ws = Application.ThisWorkbook.ActiveSheet
    
    'Your Code Goes Here
    'Call ReleaseTheKracken()
    'etc.
    
    ws.Activate
    
    With ws
        .Cells(1, 1).Select
    End With
End Sub
 
Upvote 0
Hi Peter,

At the start of your code, store the worksheet that you want to remain on. Then perform your code and at the end, activate the stored worksheet and then append your example code to the bottom.

VBA Code:
Sub blahblah()
    Dim ws  As Worksheet
   
    Set ws = Application.ThisWorkbook.ActiveSheet
   
    'Your Code Goes Here
    'Call ReleaseTheKracken()
    'etc.
   
    ws.Activate
   
    With ws
        .Cells(1, 1).Select
    End With
End Sub
Thank you for helping. I run the same macro from different worksheets, and want to remain on the active worksheet and not jump to the stored worksheet.
 
Upvote 0
VBA Code:
Sub Protect_Sheets()
   
    With Sheets("Reports")
        .Unprotect "Password"
        .Columns("K:AH").EntireColumn.Hidden = True
        .Protect "Password"
    End With
   
    With Sheets("Data")
        .Unprotect "Password"
        .Columns("A:O").EntireColumn.Hidden = True
        .Visible = False
        .Protect "Password"
    End With
   
    Sheets("Procedure").Visible = False
   
    With Sheets("Daily")
        .Unprotect "Password"
        .Columns("H").EntireColumn.Hidden = True
        .Columns("J").EntireColumn.Hidden = True
        .Protect "Password"
    End With
   
    Sheets("Stats").Protect "Password"
   
   
    'I have a number of additional sheets, where i use the same "Protect" & "Unprotect" macro's, so the same button is on each of the 20 odd sheets.
    'When i activate the macro, the sheet that i am running the macro from must remain visible - the cursor must not jump to "Reports"
   
    'With Active Sheet
    'Protect Sheet
    'Remain on the active sheet, etc.
       
    'Then i also need code to protect all the sheets to the right of "reports from a '2nd' protect macro button,_
    'and the cursor must remain on the sheet where i am running the macro from, if possible please.
       
End Sub
 
Last edited by a moderator:
Upvote 0
There is nothing in that code which would change the sheet you are on initially, assuming you are not running it from the Procedure sheet
 
Upvote 0
There is nothing in that code which would change the sheet you are on initially, assuming you are not running it from the Procedure sheet
The sheet definitely changes to "Reports", irrespective of which sheet i am running the macro from.
 
Upvote 0
Try stepping through the code line by line using F8 to see where it changes sheets.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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