如何为VBA宏程序在EXCEL菜单栏上添加按钮

2024-10-30 03:46:08
推荐回答(3个)
回答(1):

'主菜单上添加自定义按钮
Sub addbtn()
Set myMenu = Application.CommandBars("worksheet menu bar")
Set Button = myMenu.Controls.Add(Type:=msoControlButton)
Button.Caption = "按钮" '按钮上的文字,填写你需要的
Button.Style = msoButtonIconAndCaption
Button.FaceId = FaceId '按钮图标,数字比如8,系统存在的
Button.OnAction = "OnAction" '按钮执行的宏名,填写你自己编写的宏的宏名
End Sub

回答(2):

Sub auto_open()
MenuBars(xlWorksheet).Reset
Set mycommandbar = CommandBars("standard")
Set mybutton = mycommandbar.Controls.Add(Type:=msoControlButton)
With mybutton
.Style = msoButtonCaption
.Caption = "资料输入" '按扭名称
.Enabled = True
.OnAction = "程序" '要运行的程序名称
End With
End Sub
Sub auto_close()
Set mycommandbar = CommandBars("standard")
For Each mybutton In mycommandbar.Controls
If mybutton.Caption = "资料输入" Then mybutton.Delete
Next
End Sub

回答(3):

视图→工具栏→自定义→命令。在左边找到宏就可以了