Мне никогда не удавалось заставить код работать в модуле ThisWorkbook
, однако я заставил его работать в стандартном модуле.Я вытащил подпрограмму CreateToolbar вместе с подпрограммами для кнопок в стандартный модуль и оставил их как частные.В модуле ThisWorkbook, который запускается на Workbook_Open
, я изменил Call CreateToolbar
на Application.Run "'" & ThisWorkbook.Name & "'!CreateToolbar"
, и он работает как положено.Для Subs, которые запускаются при нажатии кнопки, я использовал 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