陈表达VBA学习笔记-新建工作表鼠标右键菜单按钮;新建一个我的菜单按钮
设置对应的宏过程名称为 【我的菜单宏】
点击按钮弹窗信息,信息可自定义设置
详细代码如下:
Sub 新建右键菜单()Dim 菜单栏 As CommandBarCall 删除菜单Set 菜单栏 = mandBars("Cell")'在单元格菜单中添加按钮With 菜单栏.Controls.Add(Type:=msoControlButton, before:=1)'设置点击运行的宏.OnAction = "我的菜单宏".FaceId = 59.Caption = "我的菜单".Tag = "我的菜单Tag"End WithEnd SubSub 删除菜单()Dim 菜单栏 As CommandBarDim ctrl As CommandBarControlSet 菜单栏 = mandBars("Cell")' 删除标签控件For Each 菜单 In 菜单栏.ControlsIf 菜单.Tag = "我的菜单Tag" Then菜单.DeleteEnd IfNext ctrlEnd SubSub 我的菜单宏()MsgBox "大表格VX:Chen1198061299"End Sub
Sub 新建右键菜单()
Dim 菜单栏 As CommandBar
Call 删除菜单
Set 菜单栏 = mandBars("Cell")
'在单元格菜单中添加按钮
With 菜单栏.Controls.Add(Type:=msoControlButton, before:=1)
'设置点击运行的宏
.OnAction = "我的菜单宏"
.FaceId = 59
.Caption = "我的菜单"
.Tag = "我的菜单Tag"
End With
End Sub
Sub 删除菜单()
Dim 菜单栏 As CommandBar
Dim ctrl As CommandBarControl
Set 菜单栏 = mandBars("Cell")
' 删除标签控件
For Each 菜单 In 菜单栏.Controls
If 菜单.Tag = "我的菜单Tag" Then
菜单.Delete
End If
Next ctrl
End Sub
Sub 我的菜单宏()
MsgBox "大表格VX:Chen1198061299"
End Sub