在右键菜单中添加一个宏

Gab*_*ada 3 excel vba excel-vba

我想在右键菜单中添加一个创建的宏.可能吗?

非常感谢!

bre*_*tdj 6

如果您想在右键单击鼠标时向选项添加宏,则可以尝试使用此代码CreateMacro,它将Test_Macro分配给带有标题YourCode的右键菜单.

运行KillMacro以删除菜单项

Const strMacro = "YourCode"

Sub CreateMacro()
   Dim cBut
   Call KillMacro
   Set cBut = Application.CommandBars("Cell").Controls.Add(Temporary:=True)
   With cBut
       .Caption = strMacro
       .Style = msoButtonCaption
       .OnAction = "Test_Macro"
   End With
End Sub

Sub Test_Macro()
   MsgBox "I work"
End Sub

Sub KillMacro()
   On Error Resume Next
   Application.CommandBars("Cell").Controls(strMacro).Delete
End Sub
Run Code Online (Sandbox Code Playgroud)