VBA创设菜单并指定宏代码
VBA创建菜单并指定宏代码
Private Sub auto_open()
Call 建立系统工具菜单
End Sub
Sub 建立系统工具菜单()
Dim cmdBar As CommandBar
Dim cmdMenu As CommandBarPopup
Dim cmdBtn As CommandBarButton
Set cmdBar = Application.CommandBars("WorkSheet Menu Bar")
'Set cmdBar = Application.CommandBars(1)
With cmdBar
Set cmdMenu = .Controls.Add(Type:=msoControlPopup, before:=4, temporary:=True)
With cmdMenu
.Caption = "工具箱(&K)"
With .Controls.Add(Type:=msoControlButton)
.Caption = "神奇按钮(&K)"
.OnAction = "神奇按钮"
.FaceId = 185
End With
With .Controls.Add(Type:=msoControlButton)
.Caption = "显示所有工作表(D)"
.OnAction = "显示所有工作表"
.FaceId = 12
End With
With .Controls.Add(Type:=msoControlButton)
.Caption = "卸载软件(&U)"
.OnAction = "卸载"
.FaceId = 12
End With
End With
End With
End Sub
以上是excel自动建立菜单的代码,但是现在我想指定我的宏代码,也就是说点击菜单的时候可以运行我的宏代码;因为我目前的代码是通过按钮来执行的,现在我想用上面的VBA代码来连接我的按钮宏代码,应该怎么做?
------解决方案--------------------
Private Sub auto_open()
Call 建立系统工具菜单
End Sub
Sub 建立系统工具菜单()
Dim cmdBar As CommandBar
Dim cmdMenu As CommandBarPopup
Dim cmdBtn As CommandBarButton
Set cmdBar = Application.CommandBars("WorkSheet Menu Bar")
'Set cmdBar = Application.CommandBars(1)
With cmdBar
Set cmdMenu = .Controls.Add(Type:=msoControlPopup, before:=4, temporary:=True)
With cmdMenu
.Caption = "工具箱(&K)"
With .Controls.Add(Type:=msoControlButton)
.Caption = "神奇按钮(&K)"
.OnAction = "神奇按钮"
.FaceId = 185
End With
With .Controls.Add(Type:=msoControlButton)
.Caption = "显示所有工作表(D)"
.OnAction = "显示所有工作表"
.FaceId = 12
End With
With .Controls.Add(Type:=msoControlButton)
.Caption = "卸载软件(&U)"
.OnAction = "卸载"
.FaceId = 12
End With
End With
End With
End Sub
以上是excel自动建立菜单的代码,但是现在我想指定我的宏代码,也就是说点击菜单的时候可以运行我的宏代码;因为我目前的代码是通过按钮来执行的,现在我想用上面的VBA代码来连接我的按钮宏代码,应该怎么做?
------解决方案--------------------
Sub auto_open()
Dim cmdBar As CommandBar
Dim cmdMenu As CommandBarPopup
Dim cmdBtn As CommandBarButton
Set cmdBar = Application.CommandBars("WorkSheet Menu Bar")
With cmdBar
Set cmdMenu = .Controls.Add(Type:=msoControlPopup, before:=4, temporary:=True)
With cmdMenu
.Caption = "工具箱(&K)"
With .Controls.Add(Type:=msoControlButton)
.Caption = "运行自定义模块(&K)"
.OnAction = "自定义模块"
.FaceId = 185
End With
End With