Create a toggle button with If Statement

hamistasty

Board Regular
Joined
May 17, 2011
Messages
208
I'm wanting to create a macro that acts as a toggle button but isn't an actual toggle button

This is what happens when the file opens:
Code:
Private Sub Workbook_Open()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
    Application.DisplayFormulaBar = False
    Application.DisplayStatusBar = False
 
 
    Public toggle As Long
    Set toggle = 1
End Sub

This is the button (module) code:
Code:
Public Sub UnHideNavPane()
 
    If toggle = 1 Then
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
    Application.DisplayFormulaBar = True
    Application.DisplayStatusBar = True
    Set toggle = 0
 
    ElseIf toggle = 0 Then
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
    Application.DisplayFormulaBar = False
    Application.DisplayStatusBar = False
    Set toggle = 1
 
    Else
 
    End If
End Sub

The error is that toggle isn't defined. What am I doing wrong?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Maybe this :

Code:
Public Sub UnHideNavPane()

    Dim bVisible As Boolean
    
    bVisible = Not Application.CommandBars("Ribbon").Visible
    
    With Application
        .DisplayFormulaBar = bVisible
        .DisplayStatusBar = bVisible
        .ExecuteExcel4Macro ("SHOW.TOOLBAR(""Ribbon""," & bVisible & ")")
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,604
Messages
6,179,857
Members
452,948
Latest member
UsmanAli786

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