Excel 功能区中的 2007 加载项不起作用
Excel 2007 Add-in in Ribbon doesn't work
我看到了用户名 Rory 发布的一些代码,我想对其进行编辑,以便我可以 运行 从功能区的加载项中创建我的宏。在我将它们全部粘贴到 ThisWorkBook 模块后,它确实在那里添加了功能区按钮,但每次单击它时,我都会得到:
The macro may not be available in this workbook or all macros may be disabled"
我很确定我在 Excel 选项中启用了所有宏选项。下面是代码,都在"ThisWorkBook":
下
Private Const Button As String = "SomeName"
Sub Auto_Open()
Dim CmdBar As CommandBar
Dim CmdBarMenu As CommandBarControl
Dim CmdBarMenuItem As CommandBarControl
Set CmdBar = Application.CommandBars("Worksheet Menu Bar")
Set CmdBarMenu = CmdBar.Controls("Tools") ' Index 6
On Error Resume Next
Application.DisplayAlerts = False
CmdBarMenu.Controls(Button).Delete
Application.DisplayAlerts = True
On Error GoTo 0
Set CmdBarMenuItem = CmdBarMenu.Controls.Add(Type:=msoControlButton)
With CmdBarMenuItem
.Caption = Button
.OnAction = "Hello"
End With
End Sub
Sub Auto_Close()
Dim CmdBar As CommandBar
Dim CmdBarMenu As CommandBarControl
Set CmdBar = Application.CommandBars("Worksheet Menu Bar")
Set CmdBarMenu = CmdBar.Controls("Tools") ' Index 6
On Error Resume Next
Application.DisplayAlerts = False
CmdBarMenu.Controls(Button).Delete
Application.DisplayAlerts = True
On Error GoTo 0
End Sub
Sub Hello()
MsgBox ("Hello")
End Sub
所以这里的代码给你错误 '.onAction' 如下所示:
With CmdBarMenuItem
.Caption = Button
.OnAction = "Hello"
End With
这里的想法是您需要将 "Hello" 重新编程为宏。现在它正在寻找一个名为 'Hello' 的子例程,但找不到。如果您将所有代码都放在 'ThisWorkbook' 中,我建议您在此处也将其表示为强类型。下面是一个示例,如果您希望按钮调用您在 ThisWorkbook 中创建的名为 'MyClick_Event' 的例程:
With CmdBarMenuItem
.Caption = Button
.OnAction = "ThisWorkbook.MyClick_Event"
End With
您还可以从其他 VBA 工作表创建调用,例如:
With CmdBarMenuItem
.Caption = Button
.OnAction = "Sheet1.MyClick_Event"
End With
我看到了用户名 Rory 发布的一些代码,我想对其进行编辑,以便我可以 运行 从功能区的加载项中创建我的宏。在我将它们全部粘贴到 ThisWorkBook 模块后,它确实在那里添加了功能区按钮,但每次单击它时,我都会得到:
The macro may not be available in this workbook or all macros may be disabled"
我很确定我在 Excel 选项中启用了所有宏选项。下面是代码,都在"ThisWorkBook":
下Private Const Button As String = "SomeName"
Sub Auto_Open()
Dim CmdBar As CommandBar
Dim CmdBarMenu As CommandBarControl
Dim CmdBarMenuItem As CommandBarControl
Set CmdBar = Application.CommandBars("Worksheet Menu Bar")
Set CmdBarMenu = CmdBar.Controls("Tools") ' Index 6
On Error Resume Next
Application.DisplayAlerts = False
CmdBarMenu.Controls(Button).Delete
Application.DisplayAlerts = True
On Error GoTo 0
Set CmdBarMenuItem = CmdBarMenu.Controls.Add(Type:=msoControlButton)
With CmdBarMenuItem
.Caption = Button
.OnAction = "Hello"
End With
End Sub
Sub Auto_Close()
Dim CmdBar As CommandBar
Dim CmdBarMenu As CommandBarControl
Set CmdBar = Application.CommandBars("Worksheet Menu Bar")
Set CmdBarMenu = CmdBar.Controls("Tools") ' Index 6
On Error Resume Next
Application.DisplayAlerts = False
CmdBarMenu.Controls(Button).Delete
Application.DisplayAlerts = True
On Error GoTo 0
End Sub
Sub Hello()
MsgBox ("Hello")
End Sub
所以这里的代码给你错误 '.onAction' 如下所示:
With CmdBarMenuItem
.Caption = Button
.OnAction = "Hello"
End With
这里的想法是您需要将 "Hello" 重新编程为宏。现在它正在寻找一个名为 'Hello' 的子例程,但找不到。如果您将所有代码都放在 'ThisWorkbook' 中,我建议您在此处也将其表示为强类型。下面是一个示例,如果您希望按钮调用您在 ThisWorkbook 中创建的名为 'MyClick_Event' 的例程:
With CmdBarMenuItem
.Caption = Button
.OnAction = "ThisWorkbook.MyClick_Event"
End With
您还可以从其他 VBA 工作表创建调用,例如:
With CmdBarMenuItem
.Caption = Button
.OnAction = "Sheet1.MyClick_Event"
End With