How to auto hide ribbon but only for a specific wo

2020-05-01 07:34发布

How to auto hide ribbon but only for a specific workbook and only for one worksheet in that workbook when workbook opens?

When I use the below code is working as expected and hide the ribbon but when I open another excel files the ribbon in the files is also hidden.

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

标签: excel vba
1条回答
Ridiculous、
2楼-- · 2020-05-01 08:15

Name the worksheet in a unique way e.g."aaa" and then write inside the ThisWorkbook module the following:

Private Sub Workbook_Activate()
    If ActiveSheet.Name = "aaa" Then
        'Application.CommandBars.ExecuteMso "HideRibbon"
        Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)" 'code to hide ribbon.
    Else
        Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)" 'code to show ribbon.
    End If
End Sub

Private Sub Workbook_Deactivate()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)" 'code to show ribbon.
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If Sh.Name = "aaa" Then
        'Application.CommandBars.ExecuteMso "HideRibbon"
        Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)" 'code to hide ribbon.
    Else
        Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)" 'code to show ribbon.
    End If
End Sub

which is tested in Excel 2007 and Excel 2010 and works fine.

By the way, having tested Application.CommandBars.ExecuteMso "HideRibbon" in Excel 2007 and Excel 2010, throws an error, which version do you use?

If you prefer, replace the above 'hide/show lines', with those that work in your version according to the comments.

查看更多
登录 后发表回答