命令栏的代码在标准模块,但并非在“的ThisWorkbook”模块

问题描述 投票:0回答:1

我有一个创建与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.我要确保所有启用宏所以这不是问题。

话虽这么说,我的控件/按钮被设置如下:

  • 首先控制:A键,名为get交易。这种控制完美的作品的ThisWorkbook模块和标准模块中。
  • 其次控制:所谓的表操作的是包含了三个子控件按钮,一个弹出。这就是问题所在。我有一个由OnAction属性调用被点击3次按钮的任何时候子过程,但我收到上述错误。由每个3个按钮的调用每个子直接置于下面的CreateToolbar模块的ThisWorkbook子。我已经试过了,没有成功OnAction以下格式: OnAction = "SubName" OnAction = "'SubName'" OnAction = ' & ThisWorkbook.Name & "'!SubName"

控件的图片:

enter image description here

enter image description here

下面是我的代码:

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

任何想法,建议或答案将不胜感激。

excel vba module
1个回答
0
投票

我从来没有能够得到的代码在ThisWorkbook模块运行,但我没有得到它的标准模块中工作。我把CreateToolbar子与按钮子程序一起到一个标准的模块,并把它们作为私有。放在ThisWorkbook模块,其触发的Workbook_Open,我改变Call CreateToolbarApplication.Run "'" & ThisWorkbook.Name & "'!CreateToolbar"和它的作品如预期。对于那些按钮点击运行替补,我用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
© www.soinside.com 2019 - 2024. All rights reserved.