通过VBA自定义向Excel添加工具栏
2009-05-05 17:36
591 查看
Office由于提供了VBA,为大家开发一些定制功能提供了一种途径。但是如何实现工具栏中的命令与宏进行绑定,对于初学则来说是一个不小的门槛。
今天,给大家介绍一下在Excel里写完宏后,如何通过宏自动生成工具栏。
如图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/tomyi/EntryImages/20090218/zyiToolbar.jpg)
在VBA中将要用到CommandBar,CommandBarButton两个对象。
Option Explicit
'定义全局变量
Private zyi_Bar As CommandBar
Private zyi_ComBarBtn As CommandBarButton
'-------------------------------------------------------------------------------------------------------------
'增加工具栏
'-------------------------------------------------------------------------------------------------------------
Sub AddToolBar()
'
'
'
' Application.CommandBars.Add(Name:="zy").Visible = True
Dim strBarName As String
Dim strParam As String
Dim strCaption As String
Dim strCommand As String
Dim nIndex As Integer
Dim nFaceId As Integer
Dim cBar As CommandBar
strBarName = "ZYI_TOOL"
For Each cBar In Application.CommandBars
If cBar.Name = strBarName Then
Set zyi_Bar = cBar
GoTo 20
End If
Next
'On Error GoTo 10
'Set zyi_Bar = Application.CommandBars(strBarName)
'If zyi_Bar.Name = strBarName Then
' GoTo 20 '已经存在
' zyi_Bar.Delete
'End If
'10:
On Error GoTo 100
Set zyi_Bar = Application.CommandBars.Add(Name:=strBarName)
20:
zyi_Bar.Visible = True
On Error GoTo 100
'-----------------------------------------------------------
'1. 复制工作表
nIndex = 1
strCaption = "复制工作表"
strParam = "复制工作表的单元格内容及格式!"
strCommand = "复制工作表"
nFaceId = 271
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
'-----------------------------------------------------------
'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
到此,一个来工具栏的宏大功告成了。
今天,给大家介绍一下在Excel里写完宏后,如何通过宏自动生成工具栏。
如图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/tomyi/EntryImages/20090218/zyiToolbar.jpg)
在VBA中将要用到CommandBar,CommandBarButton两个对象。
Option Explicit
'定义全局变量
Private zyi_Bar As CommandBar
Private zyi_ComBarBtn As CommandBarButton
'-------------------------------------------------------------------------------------------------------------
'增加工具栏
'-------------------------------------------------------------------------------------------------------------
Sub AddToolBar()
'
'
'
' Application.CommandBars.Add(Name:="zy").Visible = True
Dim strBarName As String
Dim strParam As String
Dim strCaption As String
Dim strCommand As String
Dim nIndex As Integer
Dim nFaceId As Integer
Dim cBar As CommandBar
strBarName = "ZYI_TOOL"
For Each cBar In Application.CommandBars
If cBar.Name = strBarName Then
Set zyi_Bar = cBar
GoTo 20
End If
Next
'On Error GoTo 10
'Set zyi_Bar = Application.CommandBars(strBarName)
'If zyi_Bar.Name = strBarName Then
' GoTo 20 '已经存在
' zyi_Bar.Delete
'End If
'10:
On Error GoTo 100
Set zyi_Bar = Application.CommandBars.Add(Name:=strBarName)
20:
zyi_Bar.Visible = True
On Error GoTo 100
'-----------------------------------------------------------
'1. 复制工作表
nIndex = 1
strCaption = "复制工作表"
strParam = "复制工作表的单元格内容及格式!"
strCommand = "复制工作表"
nFaceId = 271
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
'-----------------------------------------------------------
'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
到此,一个来工具栏的宏大功告成了。
相关文章推荐
- 在 Excel 中自定义菜单栏和工具栏-添加、删除、禁止等
- 通过VBA在Excel中添加菜单和菜单项按钮(Excel启动时候添加)
- VBA添加Excel工具栏按钮
- Excel开发(VBA)— 在Excel里自动添加工具栏
- ASP通过VBA方式调用EXCEL模版生成新的EXCEL (转)
- 为 Android 添加 Java 层服务也就是添加自定义的aidl服务到serviceManager 通过ServiceManager.getService取
- HOWTO:通过 VBA 在 FrontPage 2003 中建立自定义 HTML 代码检查程序
- Java通过POI为Excel添加数据验证
- EXCEL-VBA:通过SQL查询数据Sheet中的数据
- OC-向table中添加数据&table的编辑模式&通过封装自定义cell
- Pixhawk---通过串口方式添加一个自定义传感器(超声波为例)
- PIXHAWK源码分析之三---通过串口方式添加一个自定义传感器(1)
- VBA中用自定义函数在工作表中添加动态图表
- Win7如何自定义鼠标右键菜单 添加新建EXCEL文档
- 关于Excel下通过VBA实现工作簿文件下工作表的合并
- Excel 2010 中为自定义函数参数添加描述
- SDI在自定义的工具栏上添加下拉控件
- 添加新的Excel工具栏,实现自己需要的功能。
- Excel VBA - 自定义数据类型及其它
- 通过使用脚本修改注册表实现非界面添加excel数据源