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

如何为VBA宏程序在EXCEL菜单栏上添加按钮,第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

工具:Office2013

方法如下:

选择要设置下拉菜单的区域,点击数据中的“数据验证”:

在允许下,选择“序列”,在来源中输入下拉菜单内容,注意用英文逗号分隔,完成后,点击确定:

这样,就在Excel中制作好了下拉菜单:

新建工具栏实例

Sub 新建工具栏()

Application.CommandBars.Add(Name:="我的工具栏").Visible = True '新建一个自定义工具栏,名为我的工具栏,并可见。

Application.CommandBars("我的工具栏").Controls.Add Type:=msoControlButton, ID:=3, Before:=1 '添加保存按钮,在第一个位置

Application.CommandBars("我的工具栏").Controls.Add Type:=msoControlButton, ID:=1849, Before:=2 '添加查找按钮,在第二个位置

Application.CommandBars("我的工具栏").Controls.Add Type:=msoControlButton, ID:=295, Before:=3 '添加插入单元格按钮

Application.CommandBars("我的工具栏").Controls.Add Type:=msoControlButton, ID:=395, Before:=4 '添加货币样式按钮

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11862267.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存