VBA switches sheets running in excel 2019 v 2010+2007+2011 for MAC, where it does not

Ramses505

New Member
Joined
Oct 17, 2016
Messages
35
Office Version
  1. 2010
Platform
  1. Windows
I have some vba code to protect and unprotect the sheets in my workbook. This code runs fine (IE as expected) on Excel for Windows 2007, 2010 and Excel for MAC 2011 but in Excel for Windows 2019 it switches sheets when run.

The Workbook has 6 sheets and the code below runs from a button on Sheet6;
Code:
     <code>  Call S_Off                ' Turn Off Calc & Screen Updates
  Sheet1.Unprotect Password:=""                    '
  Sheet2.Unprotect Password:=""                    '
  Sheet3.Unprotect Password:=""                    '
  Sheet4.Unprotect Password:=""                    '
  Sheet5.Unprotect Password:=""                    '
  Sheet6.Unprotect Password:=""                    '
  ' Reflect Change on Control Button
  Sheet6.Buttons("Button_Protect").Text = "Protect OFF"
  Sheet6.Buttons("Button_Protect").Font.ColorIndex = 3
  Call S_On
</code>
What happens normally (on 2007,2010 & 2011 for MAC) is that after running the code, the button text changes and we are still on the sheet with the button (sheet6); in 2019, the code runs but we are then on Sheet5.

Call S_Off and Call S_On call code like this;
Code:
<code>Sub S_Off()                                        ' Turn Updating & Calculation OFF
  Application.ScreenUpdating = False               '
  Application.Calculation = xlCalculationManual    '
End Sub                                            '

Sub S_On()                                         ' Turn Updating & Calculation ON
  Application.Calculation = xlCalculationAutomatic '
  Application.ScreenUpdating = True                '
End Sub</code>
<code>
</code>
But commenting those calls out makes NO difference to the result.

The button that calls the ‘Unprotect code looks like this;
<code>
Code:
Sub Button_Toggle_Protection() </code>

<code>  If Sheet4.ProtectContents = True Then
    Call Protect_Off(False)
  Else
    Call Protect_On(False)
    If Application.EnableEvents = False Then
      Call Button_Toggle_Events
    End If
  End If
End Sub
File-Copy-icon.png
</code>
So it calls Protection_Off if Sheet4 is currently protected, or Protection_On if Sheet4 is not currently protected allowing the protection status to be toggled on and off based on the status of Sheet4; I only add this explanation for completeness, I don’t think it has anything to do with the problem.

When the call to Protection_On is made, the problem of switching sheets does NOT happen.

I have tested commenting out all other code except turning the Protection_Off and the problem persists.

Any ideas on why the difference most welcome.

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
try

Code:
    Call S_Off                [I][COLOR=#006400]' Turn Off Calc & Screen Updates[/COLOR][/I]
        Sheet1.Unprotect Password:=""                    '
        Sheet2.Unprotect Password:=""                    '
        Sheet3.Unprotect Password:=""                    '
        Sheet4.Unprotect Password:=""                    '
        Sheet5.Unprotect Password:=""                    '
        Sheet6.Unprotect Password:=""                    '
       [I][COLOR=#006400] ' Reflect Change on Control Button[/COLOR][/I]
       [COLOR=#ff0000] Sheet6.Activate[/COLOR]
        Sheet6.Buttons("Button_Protect").Text = "Protect OFF"
        Sheet6.Buttons("Button_Protect").Font.ColorIndex = 3
    Call S_On
 
Upvote 0
Leave the screen update on and run the code in debug mode one step at a time to observe the behavior.
See what happens and when.
I cannot test this on 2019.
A simple workaround would be to activate the sheet with the button after all is done.
 
Upvote 0
You should know the rules on cross-posting after 3 years, so please comply with them. Thank you! :)
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

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