Excel的工具栏和菜单栏VBA源码实例_第1页
Excel的工具栏和菜单栏VBA源码实例_第2页
Excel的工具栏和菜单栏VBA源码实例_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

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

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

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

5、tionTemporaryCaptionOnActionFaceIDStyleEnableTop/Left/Width/HightBeginGroupControlsAdd方法Findcontrols方法下面将通过实例来解释上述属性、参数和方法的运用。三、实例代码1、建立一命令栏即建立了一个工具栏。一般的,我们会相应的定义一个CommandbarM象来操作这个自定义工具栏,如下代码:SubAddCommandBar1(),添加一自定义工具栏DimcmdBarAsCommandBarEndSub但,Excel好像任何变化,这是因为自定义工具栏的默认Visible为False。SubAddComm

6、andBar2()添加一自定义工具栏,并显示DimcmdBarAsCommandBarEndSub2、Position示例Position:默认值为msoBarFloating常量说明msoBarLeft、msoBarTop、msoBarRight和msoBarBottom指定新命令栏的左侧、顶部、右侧和底部坐标msoBarFloating指定新命令栏不固定msoBarPopup指定新命令栏为快捷菜单msoBarMenuBar仅适用于Macintosh机SubAddCommandBar3()DimcmdBarAsCommandBarWithcmdBar.Name=MyBar.Visible=T

7、rue.Position=msoBarTopEndWithEndSubSubAddCommandBar4()DimcmdBarAsCommandBarEndSub为了避免出现重复的自定义工具栏,常规的代码写法是先删除工具栏后,再添加。SubAddCommandBar5()DimcmdBarAsCommandBarCallDeleteCommandBarEndSub|SubDeleteCommandBar()OnErrorResumeNextEndSub|3、CommandBarControlsType示例接下来我们介绍CommandBarControl对象CommandBarControl对象

8、与CommandBarButton、CommandBarComboBoXA及CommandBarPopup对象具有同样的属性和方法.SubAddCmdCtlType()DimcmdBarAsCommandBarDimcmdBtnAsCommandBarButtonDimcmdComboAsCommandBarComboBoxDimcmdPopAsCommandBarPopupCallDeleteCtlWithcmdBar.Visible=TrueWithcmdBtn.Caption=Button.Style=msoButtonCaptionEndWithWithcmdPop.Caption=P

9、opupEndWithWithcmdCombo.Caption=ComboEndWithEndWithEndSubSubDeleteCtl()OnErrorResumeNextEndSub4、Width、Height示例SubAddButtonHight()DimcmdBarAsCommandBarDimcmdBtnAsCommandBarButtonCallDeleteBtnWithcmdBar.Visible=TrueWithcmdBtn.Caption=HightShow.Style=msoButtonCaption.Height=50EndWithEndWithEndSubSubDel

10、eteBtn()OnErrorResumeNextOnErrorGoTo0EndSub5、内置FacelD、OnAction和Style在CommandBarButton中的示例SubAddCmdButton()DimcmdBarAsCommandBarDimcmdBtnAsCommandBarButtonDimcmdBtn2AsCommandBarButtonCallDeleteBtnWithcmdBar.Visible=TrueWithcmdBtn.Caption=Button1.FaceId=12.OnAction=ButtonShow1.Style=msoButtonIconAndCa

11、ptionEndWithWithcmdBtn2.Caption=Button2.FaceId=13.OnAction=ButtonShow2”.Style=msoButtonIconAndCaptionEndWithEndWithEndSubSubButtonShow1()MsgBoxButton1testEndSubSubButtonShow2()|MsgBoxButton2testEndSub6、利用个性图案制作自己的FaceID参考:7、CommandBarType示例:Popup参考:、CommandBar、CommandBarPopup8、建立一菜单栏SubAddMenuBar()D

12、imcmdBarAsCommandBarDimcmdMenuAsCommandBarPopupDimcmdBtnAsCommandBarButtonCallDeleteMenuBarWithcmdBarWithcmdMenu.Caption=MyMenuWithcmdBtn.Caption=Iteml.OnAction=ItemlAction.FaceId=12EndWithEndWithEndWithEndSubSubItem1Action()|MsgBoxMenuItemtestEndSubSubDeleteMenuBar()OnErrorResumeNextEndSub9、利用内置命令制作自己的菜单SubAddMenuBar2()DimcmdBarAsCommandBarDimcmdMenuAsCommandBarPopupDimcmdBtnAsCommandBarButtonDimcmdBuiltInBtnAsCommandBarButtonDimcmdBuiltInBtn2AsCommandBarPopupCallDelete

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论