
在 Excel 中录入好数据以后经常需要用到按钮控件再进行扩充功能。或许有朋友不知道如何添加按钮,如果不懂的朋友不妨学习一番。下面是我带来的关于excel2007 vba如何加按钮的教程,希望阅读过后对你有所启发!
excel2007 vba如何加按钮的教程添加按钮步骤1:打开Excel软件,在工具栏空白处,右键点击,在d出的选项中,选择“自定议快速访问工具栏”。
excel2007 vba如何加按钮的教程图1
添加按钮步骤2:切换到“自定义功能区”,并在右侧选择开发工具,然后确定。
excel2007 vba如何加按钮的教程图2
添加按钮步骤3:这样之后就以在菜单栏中看到开发工具了,切换开发工具界面下,选择插入工具,可以看有两个按钮图标。
excel2007 vba如何加按钮的教程图3
excel2007 vba如何加按钮的教程图4
点击开始——选项——快速访问工具栏——从下列位置选择命令(宏)——选择需要添加的”宏“——添加——确定。
鼠标左键点击快速访问工具栏的快捷键即可执行相应的宏命令。
1)点击:开发工具——控制——插入——表单控制(按钮(窗体控制键))
2)在表格中适当位置拖动,绘制快捷按钮;
3)选取按钮对应的宏,点击确定;
4)左键单击快捷按钮,修改快捷按钮名称;
5)单击宏,运行宏命令。
End If'-----------------------------------------------------------
'2. 合并单元格
nIndex = 2
strCaption = '合并单元格'
strParam = '合并单元格以及居中'
strCommand = '合并单元格'
nFaceId = 29
If zyi_Bar.Controls.Count <nIndex Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
ElseIf zyi_Bar.Controls(nIndex).Caption <>strCaption Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
End If
'-----------------------------------------------------------
'3. 居中
nIndex = 3
strCaption = '居中'
strParam = '水平垂直居中'
strCommand = '居中单元格'
nFaceId = 482
If zyi_Bar.Controls.Count <nIndex Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
ElseIf zyi_Bar.Controls(nIndex).Caption <>strCaption Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
End If
'-----------------------------------------------------------
'4. 货币
nIndex = 4
strCaption = '货币'
strParam = '货币'
strCommand = '货币'
nFaceId = 272
If zyi_Bar.Controls.Count <nIndex Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
ElseIf zyi_Bar.Controls(nIndex).Caption <>strCaption Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
End If
'-----------------------------------------------------------
'5. 将货币数字转换为大写
nIndex = 5
strCaption = '删除列'
strParam = '删除列'
'宏名称
strCommand = '删除列'
nFaceId = 1668
If zyi_Bar.Controls.Count <nIndex Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
ElseIf zyi_Bar.Controls(nIndex).Caption <>strCaption Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
End If
nIndex = nIndex + 1
While nIndex <zyi_Bar.Controls.Count
zyi_Bar.Controls(nIndex).Delete
Wend
'-----------------------------------------------------------
'6. 分割条
zyi_Bar.Controls(zyi_Bar.Controls.Count).BeginGroup = True
'-----------------------------------------------------------
'7. 将货币数字转换为大写
nIndex = 6
strCaption = '人民币'
strParam = '人民币由数字转换为大写'
'宏名称
strCommand = 'To大写人民币'
nFaceId = 384
If zyi_Bar.Controls.Count <nIndex Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
ElseIf zyi_Bar.Controls(nIndex).Caption <>strCaption Then
AddComBarBtn strParam, strCaption, strCommand, nIndex, nFaceId
End If
nIndex = nIndex + 1
While nIndex <zyi_Bar.Controls.Count
zyi_Bar.Controls(nIndex).Delete
Wend
'-----------------------------------------------------------
'9. 分割条
zyi_Bar.Controls(zyi_Bar.Controls.Count).BeginGroup = True
100:
End Sub
'-------------------------------------------------------------------------------------------------------------
'向工具栏动态添加按钮
'-------------------------------------------------------------------------------------------------------------
Sub AddComBarBtn(strParam As String, strCaption As String, strCommand As String, nIndex As Integer, nFaceId As Integer)
'
Set zyi_ComBarBtn = zyi_Bar.Controls.Add( _
ID:=1, _
Parameter:=strParam, _
Before:=nIndex, _
Temporary:=True)
With zyi_ComBarBtn
.Caption = strCaption
.Visible = True
.OnAction = strCommand
.FaceId = nFaceId
End With
End Sub
通过以上两个函数,就可以实现自动添加工具栏及按钮。
剩下将在Workbook_Open函数里调用AddToolBar,即可实现文件打开就会显示工具栏。如果仅作为工具存放,则可以把该文件保存为模版文件,即xxx.xla。
Private Sub Workbook_Open()
' MsgBox '欢迎使用Excel', vbInformation + vbOKOnly, '增强工具'
Application.StatusBar = '欢迎使用增强工具:zyi'
'显示工具栏
AddToolBar
End Sub
到此,一个来工具栏的宏大功告成了。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报。
展开剩余内容
打开APP阅读全文并永久保存
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)