Want to hide the ribbon on every opened excel file - VBA

MistakesWereMade

Board Regular
Joined
May 22, 2019
Messages
103
My code below opens an excel file using a user form. My code for some reason hides the ribbon every OTHER time it opens an excel file rather than every consecutive time. Am I missing something? Is there a way to narrow down the hide ribbon to the new opened workbook and active sheet?

Code:
Private Sub CommandButton1_Click()
    
    Dim Wbk As Workbook
    Dim Pth As String
    Dim myRange As Range
    
    Set myRange = ThisWorkbook.Worksheets("Data").Range("E2")
    
    Pth = Environ("Userprofile") & "\Desktop\Project 1 - Master Folder\Packaging Specs\"


    OpeningVar = Me.ComboBox1.Value


    Me.ComboBox1.Clear
    myRange.Clear
    
    On Error Resume Next
    
    Set Wbk = Workbooks.Open(Pth & OpeningVar)
    
    CommandBars.ExecuteMso "HideRibbon"
    
    Wbk.ActiveSheet.Range("A1").Select
    
    On Error GoTo 0
   
    If Wbk Is Nothing Then
        MsgBox "Workbook not found."
    End If
    
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I'm not 100% sure, but I think that it's due to that the "HideRibbon" is like a toggle button.
First time the code is run it hides the ribbon, next time it switch it back to not hidden.
 
Upvote 0
Yeah, I think it was probably a toggle issue. So I came up with this If loop and it fixed my problems. I wanted to hide the ribbon for newly opened files but not my main workbook when I reveal it. Below is my final code.

Code:
Private Sub CommandButton1_Click()
    
    Dim Wbk As Workbook
    Dim Pth As String
    Dim myRange As Range
    
    Set myRange = ThisWorkbook.Worksheets("Data").Range("E2")
    
    Pth = Environ("Userprofile") & "\Desktop\afa\BIJL8\"


    OpeningVar = Me.ComboBox1.Value


    Me.ComboBox1.Clear
    myRange.Clear
    
    On Error Resume Next
    
    Set Wbk = Workbooks.Open(Pth & OpeningVar)
    
    With Wbk
    
        ActiveWindow.Zoom = 95
    
        Wbk.ActiveSheet.Range("A1").Select
    
        If Application.CommandBars("Ribbon").Height >= 150 Then
            CommandBars.ExecuteMso "HideRibbon"
        End If
        
    End With
    
    On Error GoTo 0
   
    If Wbk Is Nothing Then
        MsgBox "Workbook not found."
    End If
    
End Sub




Private Sub CommandButton3_Click()


Application.Visible = True


    If Application.CommandBars("Ribbon").Height <= 150 Then
        CommandBars.ExecuteMso "HideRibbon"
    End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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