VBA auto hide ribbon in Excel 2013

前端 未结 7 2092
慢半拍i
慢半拍i 2020-12-14 12:13

How to Auto-hide Ribbon in Excel 2013 in VBA? I would like to achieve exactly what I get by clicking on the upper arrow icon at the right top of Excel menu mark

相关标签:
7条回答
  • 2020-12-14 12:17

    Give this a try:

    Sub ShowHideRibbon()
    
    If CommandBars("Ribbon").Controls(1).Height < 100 Then
        CommandBars.ExecuteMso ("MinimizeRibbon")
    Else
        CommandBars.ExecuteMso ("MinimizeRibbon")
    End If
    
    End Sub
    

    Or this:

    Sub ShowHideRibbon1()
    
    If Application.ExecuteExcel4Macro("Get.ToolBar(7,""Ribbon"")") Then
        Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"", False)"
    Else
        Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"", True)"
    End If
    
    End Sub
    
    0 讨论(0)
  • 2020-12-14 12:24

    I can't see that anyone else has brought this up... This isn't a workaround, this is the actual idMSO for what I think you're looking for. This code makes my excel window look like everything is gone the same way the first option does for Auto-Hide Ribbon.

    Before the code runs, my window looks like this, in the 'Restore' size:

    Running the following code:

    Sub HideTheRibbon()
        CommandBars.ExecuteMso "HideRibbon"
    End Sub
    

    Will make your window look like this, in the maxamized window size (just like what would happen if you were to press the Auto-Hide Ribbon button manually):

    If you want the ribbon automatically hidden when the workbook opens, put this in the workbook code:

    Sub Workbook_Open()
        CommandBars.ExecuteMso "HideRibbon"
    End Sub
    

    Alternatively, to achieve the same thing, you could put this code in a module:

    Sub Auto_Open()
        CommandBars.ExecuteMso "HideRibbon"
    End Sub
    

    If you want the window to revert back to normal, you run the exact same code again. In other words, the following code would make no visual change at all when ran because the idMSO "HideRibbon" is a toggleButton:

    Sub HideTheRibbon()
        CommandBars.ExecuteMso "HideRibbon"
        CommandBars.ExecuteMso "HideRibbon"
    End Sub
    

    If you want a full list of all the idMSO in excel, click the following that apply to you: Excel 2013+, Excel 2010, Excel 2007

    0 讨论(0)
  • 2020-12-14 12:25

    To get this code to work in excel 2016 you will need the following code in the "ThisWorkbook" mode.

    Credit goes to BigBen - not me

    Private Sub Workbook_Open()
    application.CommandBars.ExecuteMso "HideRibbon"
    End Sub
    
    0 讨论(0)
  • 2020-12-14 12:29

    I call this macro on Workbook_Open to check for the ribbon and if not hidden, it will hide the ribbon (I actually have it located in another Sub that also removes the formula bar, status bar, headings, and gridlines at Workbook_Open)...

    Sub HideRibbon()
    If CommandBars("Ribbon").Controls(1).Height < 100 Then
    Exit Sub
    Else
        CommandBars.ExecuteMso ("MinimizeRibbon")
    End If
    End Sub
    

    Then I call this macro on Workbook_BeforeClose to check for the ribbon and if it is not shown, it will show the ribbon for the next excel spreadsheet that is opened.

    Sub ShowRibbon()
    If CommandBars("Ribbon").Controls(1).Height > 100 Then
    Exit Sub
    Else
        CommandBars.ExecuteMso ("MinimizeRibbon")
    End If
    End Sub
    

    This eliminates the chance of hiding the ribbon when the workbook is opened and a user then manually showing the ribbon which in turn would reverse the show on close and actually hide the ribbon. On open, the ribbon would then be shown again. This will keep it the same every time on open and close of the workbook.

    0 讨论(0)
  • 2020-12-14 12:36

    Probably you should do something a little more complicated:

    Use CommandBars.ExecuteMso "MinimizeRibbon" to show/hide the ribbon. Depending on what you want, you may show/hide all other tabs in the ribbon. E.g. use something of the code here -> Excel Hide/Show all tabs on Ribbon except custom tab

    Thus 2 steps:

    Step 1 - show or hide with the CommandBars.ExecuteMso

    Step 2 - show or hide the rest of the tabs with some macros from the link.

    A little big workaround, but you will get what you want.

    0 讨论(0)
  • 2020-12-14 12:43

    I use this for presentation purposes

    ActiveWindow.DisplayGridlines = False

    ActiveWindow.DisplayHeadings = False

    Application.DisplayFormulaBar = False

    Application.DisplayFullScreen = True This is what i used to hide the ribbon

    0 讨论(0)
提交回复
热议问题