excel的工具栏和菜单栏vba源码实例

上传人:小** 文档编号:90940723 上传时间:2019-06-20 格式:DOC 页数:8 大小:87KB
返回 下载 相关 举报
excel的工具栏和菜单栏vba源码实例_第1页
第1页 / 共8页
excel的工具栏和菜单栏vba源码实例_第2页
第2页 / 共8页
excel的工具栏和菜单栏vba源码实例_第3页
第3页 / 共8页
excel的工具栏和菜单栏vba源码实例_第4页
第4页 / 共8页
excel的工具栏和菜单栏vba源码实例_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《excel的工具栏和菜单栏vba源码实例》由会员分享,可在线阅读,更多相关《excel的工具栏和菜单栏vba源码实例(8页珍藏版)》请在金锄头文库上搜索。

1、工具栏和菜单栏工具栏和菜单栏的运用更多时候是伴随着加载宏和个性Excel界面的出现而出现。在不断加深对Excel VBA的理解和运用,我们编程的思路渐渐会转到考虑代码的通用性和应用方案上,将代码和Excel数据源分开。因此,制作更多具有通用功能的加载宏(不管是xla加载宏,还是Com加载宏),可以最大极限的发挥VBA编程的魅力,而不是要求用户强制启用宏。也正是因为这个原因,在我们去学习工具栏和菜单栏时,要明白的一个道理是,制作工具栏仅仅是为了加载宏等具体运用的实现,不要一味地去追求工具栏的花哨。一、几个基本概念在开始本节之前,先理解什么是命令栏?命令栏(CommandBars):是工具栏、菜单

2、栏和快捷菜单的统称。工具栏:带有按钮和选项的工具条,使用这些按钮和选项可执行命令。如下图:菜单栏:标题栏下的水平栏,包括菜单名称。如下图快捷菜单:又叫弹出式菜单,鼠标右键单击。如下图。二、CommandBars集合对象通过上面几幅图片的直观概念之后,我们接下来理解CommandBar集合。所有的工具栏和菜单栏代码都是围绕Commandbars集合展开的。CommandBarControls集合包含三种类型控件。CommandBarButton:代表命令栏中的一个按钮控件(按钮控件:工具栏上的按钮,或菜单、子菜单或快捷菜单上的菜单项,当单击它们时会运行一条命令。工具栏按钮和菜单项共享相同的属性和

3、方法。)。该控件的 Type 属性必须是 msoControlButton。)CommandBarComboBox:代表命令栏中的一个组合框控件(组合框控件:菜单栏、工具栏、菜单、子菜单或快捷菜单上的自定义编辑框、下拉列表框或组合框。当工具栏垂直停靠时,它所包含的任何自定义组合框控件都不可见。)。该控件的 Type 属性必须是 msoControlEdit、msoControlDropdown、msoControlComboBox、msoControlButtonDropdown、msoControlSplitDropdown、msoControlOCXDropdown、msoControlG

4、raphicCombo 或 msoControlGraphicDropdown。)CommandBarPopup:代表命令栏中的一个弹出式控件(弹出式控件:是菜单栏或工具栏上的内置或自定义控件,当单击它时显示菜单,或者是菜单、子菜单、或快捷菜单上的内置或自定义菜单项,当指针放在其上时显示子菜单。)。该控件的 Type 属性必须是 msoControlPopup、msoControlGraphicPopup、msoControlButtonPopup、msoControlSplitButtonPopup 或 msoControlSplitButtonMRUPopup。几种常见属性,参数和方法:V

5、isibleNameTypePostionTemporaryCaptionOnActionFaceIDStyleEnableTop/Left/Width/HightBeginGroupControlsAdd方法Findcontrols方法下面将通过实例来解释上述属性、参数和方法的运用。三、实例代码1、 建立一命令栏Application.CommandBars.Add 即建立了一个工具栏。一般的,我们会相应的定义一个Commandbar对象来操作这个自定义工具栏,如下代码:Sub AddCommandBar1() 添加一自定义工具栏 Dim cmdBar As CommandBar Set c

6、mdBar = Application.CommandBars.AddEnd Sub但,Excel好像任何变化,这是因为自定义工具栏的默认Visible为False。Sub AddCommandBar2() 添加一自定义工具栏,并显示 Dim cmdBar As CommandBar Set cmdBar = Application.CommandBars.Add cmdBar.Visible = TrueEnd Sub2、 Position示例Position: 默认值为 msoBarFloating常量 说明msoBarLeft、msoBarTop、msoBarRight 和 msoBar

7、Bottom 指定新命令栏的左侧、顶部、右侧和底部坐标msoBarFloating 指定新命令栏不固定msoBarPopup 指定新命令栏为快捷菜单msoBarMenuBar 仅适用于 Macintosh 机Sub AddCommandBar3() Dim cmdBar As CommandBar Set cmdBar = Application.CommandBars.Add(, , , Temporary:=True) With cmdBar .Name = My Bar .Visible = True .Position = msoBarTop End WithEnd SubSub Ad

8、dCommandBar4() Dim cmdBar As CommandBar Set cmdBar = Application.CommandBars.Add(Name:=My Bar, Position:=msoBarTop, Temporary:=True) cmdBar.Visible = TrueEnd Sub为了避免出现重复的自定义工具栏,常规的代码写法是先删除工具栏后,再添加。Sub AddCommandBar5() Dim cmdBar As CommandBar Call DeleteCommandBar Set cmdBar = Application.CommandBar

9、s.Add(Name:=My Bar, Position:=msoBarTop, Temporary:=True) cmdBar.Visible = TrueEnd SubSub DeleteCommandBar() On Error Resume Next Application.CommandBars(My Bar).DeleteEnd Sub3、 CommandBar Controls Type示例接下来我们介绍CommandBarControl对象CommandBarControl对象与 CommandBarButton、CommandBarComboBox 以及 CommandBar

10、Popup 对象具有同样的属性和方法.Sub AddCmdCtlType() Dim cmdBar As CommandBar Dim cmdBtn As CommandBarButton Dim cmdCombo As CommandBarComboBox Dim cmdPop As CommandBarPopup Call DeleteCtl Set cmdBar = Application.CommandBars.Add(Name:=CommandControl Type, Temporary:=True) With cmdBar .Visible = True Set cmdBtn =

11、 .Controls.Add(Type:=msoControlButton) With cmdBtn .Caption = Button .Style = msoButtonCaption End With Set cmdPop = .Controls.Add(Type:=msoControlPopup) With cmdPop .Caption = Popup End With Set cmdCombo = .Controls.Add(Type:=msoControlComboBox) With cmdCombo .Caption = Combo End With End WithEnd S

12、ubSub DeleteCtl() On Error Resume Next Application.CommandBars(CommandControl Type).DeleteEnd Sub4、 Width、Height示例Sub AddButtonHight() Dim cmdBar As CommandBar Dim cmdBtn As CommandBarButton Call DeleteBtn Set cmdBar = Application.CommandBars.Add(Name:=cmdBtn Type, temporary:=True) With cmdBar .Visi

13、ble = True Set cmdBtn = .Controls.Add(Type:=msoControlButton) With cmdBtn .Caption = Hight Show .Style = msoButtonCaption .Height = 50 End With End WithEnd SubSub DeleteBtn() On Error Resume Next Application.CommandBars(cmdBtn Type).Delete On Error GoTo 0End Sub5、 内置FaceID、OnAction和Style在CommandBarButton中的示例Sub AddCmdButton() Dim cmdBar As CommandBar Dim cmdBtn As CommandBarButton Dim cmdBtn2 As CommandBarButton Call DeleteBtn Set cmdBar = Application.CommandBars.Add(Name:=cmdBtn Type, Temporary:=True) With cmdBar .Visible = True Set cmdBtn = .Controls.Add(Type:=msoControlButton) With

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号