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
34
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
</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
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,871
Office Version
  1. 365
Platform
  1. Windows
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
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,345
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,680
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
You should know the rules on cross-posting after 3 years, so please comply with them. Thank you! :)
 

Watch MrExcel Video

Forum statistics

Threads
1,114,486
Messages
5,548,350
Members
410,828
Latest member
A9Bosv3
Top