




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
工具栏和菜单栏工具栏和菜单栏 工具栏和菜单栏的运用更多时候是伴随着加载宏和个性 Excel 界面的出现而出现 在不断加深对 Excel VBA 的理解和运用 我们编程的思路渐渐会转到考虑代码的通用性和应用方案上 将代码和 Excel 数据源分开 因此 制作更多具有通用功能的加载宏 不管是 xla 加载宏 还是 Com 加载宏 可以最大 极限的发挥 VBA 编程的魅力 而不是要求用户强制启用宏 也正是因为这个原因 在我们去学习工具栏 和菜单栏时 要明白的一个道理是 制作工具栏仅仅是为了加载宏等具体运用的实现 不要一味地去追 求工具栏的花哨 一 几个基本概念一 几个基本概念 在开始本节之前 先理解什么是命令栏 命令栏 CommandBars 是工具栏 菜单栏和快捷菜单的统称 工具栏 带有按钮和选项的工具条 使用这些按钮和选项可执行命令 如下图 菜单栏 标题栏下的水平栏 包括菜单名称 如下图 快捷菜单 又叫弹出式菜单 鼠标右键单击 如下图 二 二 CommandBars 集合对象集合对象 通过上面几幅图片的直观概念之后 我们接下来理解 CommandBar 集合 所有的工具栏和菜单栏代 码都是围绕 Commandbars 集合展开的 CommandBarControls 集合包含三种类型控件 CommandBarButton 代表命令栏中的一个按钮控件 按钮控件 工具栏上的按钮 或菜单 子菜单或 快捷菜单上的菜单项 当单击它们时会运行一条命令 工具栏按钮和菜单项共享相同的属性和方法 该控件的 Type 属性必须是 msoControlButton CommandBarComboBox 代表命令栏中的一个组合框控件 组合框控件 菜单栏 工具栏 菜单 子菜 单或快捷菜单上的自定义编辑框 下拉列表框或组合框 当工具栏垂直停靠时 它所包含的任何自定义 组合框控件都不可见 该控件的 Type 属性必须是 msoControlEdit msoControlDropdown msoControlComboBox msoControlButtonDropdown msoControlS plitDropdown msoControlOCXDropdown msoControlGraphicCombo 或 msoControlGraphicDropdown CommandBarPopup 代表命令栏中的一个弹出式控件 弹出式控件 是菜单栏或工具栏上的内置或自定 义控件 当单击它时显示菜单 或者是菜单 子菜单 或快捷菜单上的内置或自定义菜单项 当指针放 在其上时显示子菜单 该控件的 Type 属性必须是 msoControlPopup msoControlGraphicPopup msoControlButtonPopup msoControlSplitButtonPopup 或 msoControlSplitButtonMRUPopup 几种常见属性 参数和方法 Visible Name Type Postion Temporary Caption OnAction FaceID Style Enable Top Left Width Hight BeginGroup Controls Add 方法 Findcontrols 方法 下面将通过实例来解释上述属性 参数和方法的运用 三 实例代码 1 建立一命令栏 Application CommandBars Add 即建立了一个工具栏 一般的 我们会相应的定义一个 Commandbar 对象来操作这个自定义工具栏 如下代码 Sub AddCommandBar1 添加一自定义工具栏 Dim cmdBar As CommandBar Set cmdBar Application CommandBars Add End Sub 但 Excel 好像任何变化 这是因为自定义工具栏的默认 Visible 为 False Sub AddCommandBar2 添加一自定义工具栏 并显示 Dim cmdBar As CommandBar Set cmdBar Application CommandBars Add cmdBar Visible True End Sub 2 Position 示例 Position 默认值为 msoBarFloating 常量 说明 msoBarLeft msoBarTop msoBarRight 和 msoBarBottom 指定新命令栏的左侧 顶部 右侧和底部坐标 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 With End Sub Sub AddCommandBar4 Dim cmdBar As CommandBar Set cmdBar Application CommandBars Add Name My Bar Position msoBarTop Temporary True cmdBar Visible True End Sub 为了避免出现重复的自定义工具栏 常规的代码写法是先删除工具栏后 再添加 Sub AddCommandBar5 Dim cmdBar As CommandBar Call DeleteCommandBar Set cmdBar Application CommandBars Add Name My Bar Position msoBarTop Temporary True cmdBar Visible True End Sub Sub DeleteCommandBar On Error Resume Next Application CommandBars My Bar Delete End Sub 3 CommandBar Controls Type 示例 接下来我们介绍 CommandBarControl 对象 CommandBarControl 对象与 CommandBarButton CommandBarComboBox 以及 CommandBarPopup 对象 具有同样的属性和方法 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 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 With End Sub Sub DeleteCtl On Error Resume Next Application CommandBars CommandControl Type Delete End Sub 4 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 Visible True Set cmdBtn Controls Add Type msoControlButton With cmdBtn Caption Hight Show Style msoButtonCaption Height 50 End With End With End Sub Sub DeleteBtn On Error Resume Next Application CommandBars cmdBtn Type Delete On Error GoTo 0 End Sub 5 内置 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 cmdBtn Caption Button1 FaceId 12 OnAction ButtonShow1 Style msoButtonIconAndCaption End With Set cmdBtn2 Controls Add Type msoControlButton With cmdBtn2 Caption Button2 FaceId 13 OnAction ButtonShow2 Style msoButtonIconAndCaption End With End With End Sub Sub ButtonShow1 MsgBox Button1 test End Sub Sub ButtonShow2 MsgBox Button2 test End Sub 6 利用个性图案制作自己的 FaceID 参考 AddCustomICO xls 7 CommandBar Type 示例 Popup 参考 CommandBar Popup1 xls CommandBar Popup2 xls CommandBar Popup Form xls 8 建立一菜单栏 Sub AddMenuBar Dim cmdBar As CommandBar Dim cmdMenu As CommandBarPopup Dim cmdBtn As CommandBarButton Call DeleteMenuBar Set cmdBar Application CommandBars WorkSheet Menu Bar Set cmdBar Application CommandBars 1 With cmdBar Set cmdMenu Controls Add Type msoControlPopup temporary True With cmdMenu Caption My Menu Set cmdBtn Controls Add Type msoControlButton With cmdBtn Caption Item1 OnAction Item1Action FaceId 12 End With End With End With End Sub Sub Item1Action MsgBox Menu Item test End Sub Sub DeleteMenuBar On Error Resume Next Application CommandBars 1 Controls My Menu Delete End Sub 9 利用内置命令制作自己的菜单 Sub AddMenuBar2 Dim cmdBar As CommandBar Dim cmdMenu As CommandBarPopup Dim cmdBtn As CommandBarButton Dim cmdBuiltInBtn As CommandBarButton Dim cmdBuiltInBtn2 As CommandBarPopup Call D
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 考研政治面试题库及答案
- 农业产业园项目2025年区域农业产业结构优化研究与效益评估
- 基于2025年视角的资源型城市产业升级与绿色发展报告
- 2025年特色主题餐厅餐饮市场区域差异与竞争策略研究报告
- 数字化驱动2025:公路货运行业效率提升与可持续发展报告
- 安全教育培训记录缺失课件
- 共享厨房在促进餐饮消费升级方面的实践与探索报告
- 服装设计师品牌方案
- 租赁物品使用协议格式
- 2025年医药电商平台药品供应链金融合规性分析与运营优化报告
- 德瑞斯D600变频器说明书
- 2025-2030年中国锂电池回收行业市场深度调研及前景趋势与投资研究报告
- 数字化教育资源在跨学科教学中的应用
- JG/T 127-2017建筑门窗五金件滑撑
- T/CGCC 7-2017焙烤食品用糖浆
- 2024福建农信社春季招聘笔试历年典型考题及考点剖析附带答案详解
- 医生重症医学科进修汇报
- DB13(J)-T 8389-2020 被动式超低能耗建筑节能工程施工及质量验收标准
- 月嫂 考试题及答案
- 物质安全资料脱模剂MSDS
- 2025年中国过敏性鼻炎市场研究报告
评论
0/150
提交评论