我有一个创建带有5个控件的CommandBar
的过程。第一个控制按钮可以正常工作,但是第二个控制按钮的3个子按钮调用的宏在单击按钮时显示以下错误:Cannot run the macro Heavy MX C D check status 2018 Rev 28 2019 Rev 1 January 2019 Working.xlsb'!SubName'. The macro may not be available in this workbook or all macros may be disabled.
我确保所有宏都已启用,所以这不是问题
话虽如此,我的控件/按钮设置如下:
ThisWorkbook
模块和标准模块中完美运行。第二个控件:名为 Sheet Actions 的弹出窗口,其中包含三个子控件按钮。这就是问题所在。我有3个子按钮中的任何一个都被单击时由OnAction
属性调用的子过程,但是我收到上述错误。每个子按钮由位于CreateToolbar
模块中ThisWorkbook
子按钮正下方的3个按钮中的每个按钮调用。我已经为OnAction
尝试了以下格式,但没有成功:
OnAction = "SubName"
OnAction = "'SubName'"
OnAction = ' & ThisWorkbook.Name & "'!SubName"
控件图片:
下面是我的代码:
Private Sub CreateToolbar()
'called from Workbook Open event procedure
Dim Cbar As CommandBar
Dim CbarControl As CommandBarControl
Dim CbarControlSub1 As CommandBarControl
Dim CbarControlSub2 As CommandBarControl
Dim CbarControlSub3 As CommandBarControl
'Get rid of any existing toolbar
Application.CommandBars(sToolbarName).Delete
'*************************************************************
'This works as inteneded
Set Cbar = Application.CommandBars.Add(Name:=sToolbarName)
'Create the new toolbar
With Cbar
'Add a toolbar command button
With .Controls.Add(Type:=msoControlButton)
.OnAction = "'" & ThisWorkbook.Name & "'!GetTransactions"
.ShortcutText = "Ctrl+Shift+G"
.Caption = "Get Transactions"
.Style = msoButtonCaption
.TooltipText = "Click to Import and Categorize transactions."
End With
.Visible = True
.Position = msoBarTop
End With
Application.MacroOptions Macro:="GetTransactions", _
HasShortcutKey:=True, _
ShortcutKey:="G"
Application.OnKey "^+g", "GetTransactions"
'*************************************************************
'*************************************************************
'This is where I am having issues
Set CbarControl = Cbar.Controls.Add(Type:=msoControlPopup)
CbarControl.Caption = "Sheet Actions"
Set CbarControlSub1 = CbarControl.Controls.Add(Type:=msoControlButton)
With CbarControlSub1
.Style = msoButtonIconAndCaption
.Caption = "Filter For New Transations"
.OnAction = "FilterForNewTrans"
.BeginGroup = True
End With
Set CbarControlSub2 = CbarControl.Controls.Add(Type:=msoControlButton)
With CbarControlSub2
.Style = msoButtonIconAndCaption
.Caption = "Clear Transaction Filter"
.OnAction = "ClearFilter"
.BeginGroup = True
End With
Set CbarControlSub3 = CbarControl.Controls.Add(Type:=msoControlButton)
With CbarControlSub3
.Style = msoButtonIconAndCaption
.Caption = "Clear Row Fill Color"
.OnAction = "ClearFillColor"
.BeginGroup = True
End With
'*************************************************************
End Sub
任何想法,建议或答案将不胜感激。
答案 0 :(得分:0)
我从来没有让代码在ThisWorkbook
模块中运行,但是我确实让它在标准模块中工作。我将CreateToolbar子按钮以及按钮的子过程拉入标准模块,并将其保留为私有。在Workbook_Open
上触发的ThisWorkbook模块中,我将Call CreateToolbar
更改为Application.Run "'" & ThisWorkbook.Name & "'!CreateToolbar"
并按预期工作。对于单击按钮运行的Sub,我使用了Application.OnKey "somekeycombination", "SomeSubName"
。
以下是带有附加按钮的CommandBar
的最终代码:
Private Sub CreateToolbar()
'called from Workbook Open event procedure
Dim Cbar As CommandBar 'ToolBar
Dim CbarControl_1 As CommandBarControl
Dim CbarControl_2 As CommandBarControl
Dim CbarControl_3 As CommandBarControl
Dim ControlSubA1 As CommandBarControl
Dim ControlSubA2 As CommandBarControl
Dim ControlSubB1 As CommandBarControl
Dim ControlSubB2 As CommandBarControl
Dim ControlSubB3 As CommandBarControl
Dim ControlSubB4 As CommandBarControl
Dim ControlSubB5 As CommandBarControl
'Get rid of any existing toolbar
On Error Resume Next
Application.CommandBars(ToolbarName).Delete
'**************************************
'Add the Toolbar
'**************************************
Set Cbar = Application.CommandBars.Add(Name:=ToolbarName)
With Cbar
.Visible = True
.Position = msoBarTop
End With
'**************************************
'********************************************************************
'Button1
'********************************************************************
Set CbarControl_1 = Cbar.Controls.Add(Type:=msoControlPopup)
CbarControl_1.Caption = "Get Transactions"
'**************************
'SubButton1: Ctrl+Shift+G
'**************************
Set ControlSubA1 = CbarControl_1.Controls.Add(Type:=msoControlButton)
With ControlSubA1
.Style = msoButtonIconAndCaption
.Caption = "Import/Categorize ALL RECENT transactions"
.OnAction = "GetCurrMonTransactions"
.ShortcutText = "Ctrl+Shift+G"
.BeginGroup = True
Application.OnKey "^+g", "GetCurrMonTransactions"
End With
'**************************
'SubButton2: Ctrl+Shift+P
'**************************
Set ControlSubA2 = CbarControl_1.Controls.Add(Type:=msoControlButton)
With ControlSubA2
.Style = msoButtonIconAndCaption
.Caption = "Import/Categorize PREVIOUS MONTH'S transactions"
.OnAction = "GetPrevMonthTransactions"
.ShortcutText = "Ctrl+Shift+P"
.BeginGroup = True
Application.OnKey "^+p", "GetPrevMonthTransactions"
End With
'********************************************************************
'********************************************************************
'********************************************************************
'Button 2: Ctrl+Shift+U
'********************************************************************
Set CbarControl_2 = Cbar.Controls.Add(Type:=msoControlButton)
With CbarControl_2
.OnAction = "'" & ThisWorkbook.Name & "'!UploadTransToSQL"
.Caption = "Save To SQL"
.ShortcutText = "Ctrl+Shift+U"
.Style = msoButtonCaption
.TooltipText = "Click to Export updated transactions to the SQL Server"
Application.OnKey "^+u", "UploadTransToSQL"
End With
'********************************************************************
'********************************************************************
'********************************************************************
'Button 3
'********************************************************************
Set CbarControl_3 = Cbar.Controls.Add(Type:=msoControlPopup)
CbarControl_3.Caption = "Sheet Actions"
'*************************
'SubButton1: Ctrl+Shift+F
'*************************
Set ControlSubB1 = CbarControl_3.Controls.Add(Type:=msoControlButton)
With ControlSubB1
.Style = msoButtonIconAndCaption
.Caption = "Filter For New Transations"
.OnAction = "FilterForNewTrans"
.ShortcutText = "Ctrl+Shift+F"
.BeginGroup = True
Application.OnKey "^+f", "FilterForNewTrans"
End With
'*************************
'SubButton2: Ctrl+Shift+O
'*************************
Set ControlSubB2 = CbarControl_3.Controls.Add(Type:=msoControlButton)
With ControlSubB2
.Style = msoButtonIconAndCaption
.Caption = "Filter for Old Updated Transactions"
.OnAction = "FilterForOldUpdates"
.ShortcutText = "Ctrl+Shift+O"
.BeginGroup = True
Application.OnKey "^+o", "FilterForOldUpdates"
End With
'***********************
'SubButton3: Ctrl+Alt+c
'***********************
Set ControlSubB3 = CbarControl_3.Controls.Add(Type:=msoControlButton)
With ControlSubB3
.Style = msoButtonIconAndCaption
.Caption = "Clear Transaction Filters"
.OnAction = "ClearFilter"
.ShortcutText = "Ctrl+Alt+c"
.BeginGroup = True
Application.OnKey "^%c", "ClearFilter"
End With
'************************
'SubButton4: Ctrl+Alt+r
'************************
Set ControlSubB4 = CbarControl_3.Controls.Add(Type:=msoControlButton)
With ControlSubB4
.Style = msoButtonIconAndCaption
.Caption = "Clear Row Fill Color"
.OnAction = "ClearFillColor"
.ShortcutText = "Ctrl+Alt+r"
.BeginGroup = True
Application.OnKey "^%r", "ClearFillColor"
End With
'************************
'SubButton5: Ctrl+Alt+a
'************************
Set ControlSubB5 = CbarControl_3.Controls.Add(Type:=msoControlButton)
With ControlSubB5
.Style = msoButtonIconAndCaption
.Caption = "Toggle formula Auto-Calculations"
.OnAction = "TurnOnAutoCalc"
.ShortcutText = "Ctrl+Alt+a"
.BeginGroup = True
Application.OnKey "^%a", "TurnOnAutoCalc"
End With
'********************************************************************
'********************************************************************
End Sub
还有ThisWorkbook
模块中的代码:
Private Sub Workbook_Open()
Application.Run "'" & ThisWorkbook.Name & "'!CreateToolbar"
End Sub
'When this workbook is the active workbook, the toolbar will be enabled and show up
Private Sub Workbook_Activate()
On Error Resume Next
With Application.CommandBars(ToolbarName)
.Enabled = True
.Visible = True
End With
End Sub
'When the user activates another workbook, this disables the command bar
Private Sub Workbook_Deactivate()
On Error Resume Next
With Application.CommandBars(ToolbarName)
.Enabled = False
.Visible = False
End With
End Sub