通过1按钮VBA EXCEL激活和取消激活功能区

时间:2017-04-27 07:21:56

标签: excel vba excel-vba

我有2个按钮的excel工作表。第一个按钮取消激活功能区,第二个按钮激活它。我需要将两个功能组合成一个按钮。 代码示例:

Private Sub CommandButton1_Click()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
    Application.DisplayFormulaBar = False
    Application.DisplayStatusBar = Not Application.DisplayStatusBar
    ActiveWindow.DisplayWorkbookTabs = False
End Sub

Private Sub CommandButton2_Click()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
    Application.DisplayFormulaBar = True
    Application.DisplayStatusBar = True
    ActiveWindow.DisplayWorkbookTabs = True
End Sub

希望得到帮助。

1 个答案:

答案 0 :(得分:1)

您需要先获取功能区的实际可见性状态。

Private Sub cmdToggleRibbon_Click()
    Dim isRibbonVisible As Boolean
    isRibbonVisible = Application.ExecuteExcel4Macro("Get.ToolBar(7,""Ribbon"")")

    If isRibbonVisible Then
        Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
    Else
        Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
    End If
    Application.DisplayFormulaBar = Not isRibbonVisible
    Application.DisplayStatusBar = Not isRibbonVisible
    ActiveWindow.DisplayWorkbookTabs = Not isRibbonVisible
End Sub