excel_vba_编程教程(完整版)-第70部分
按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
Submenu〃)ntrols _
。Add(Type:=msoControlButton; Before:=1)。Caption = 〃Option 1〃
当你按下回车键,上面的指令会在工具菜单里的My Submenu里添加命令Option 1。如果你没有
将上面的语句输入在一行的话,它就不会起作用。你可以使用相同的技术给你的子菜单里添加
更多的菜单项。
下面的过程将在内置菜单格式里添加自定义子菜单Colors,并且在里面放置四个选项:Red; Green;
Blue和Black。使用这些选项,你可以更改所选工作表单元格或者单元格区域里的文本颜色。接下
来的过程将应用适当的颜色格式。
Sub Colors()
Dim myMenu As Object
Dim mySubMenu As Object
Set myMenu = mandBars(〃Worksheet menu bar〃)ntrols(〃Format〃)
With myMenu
243
… 页面 260…
ntrols。Add(Type:=msoControlPopup; Before:=2)。Caption = 〃Colors〃
End With
Set mySubMenu = myMenuntrols(〃Colors〃)
With mySubMenu
ntrols。Add(Type:=msoControlButton)。Caption = 〃Red〃
ntrols。Add(Type:=msoControlButton)。Caption = 〃Green〃
ntrols。Add(Type:=msoControlButton)。Caption = 〃Blue〃
ntrols。Add(Type:=msoControlButton)。Caption = 〃Black〃
ntrols(〃Red〃)。OnAction = 〃ColorRed〃
ntrols(〃Green〃)。OnAction = 〃ColorGreen〃
ntrols(〃Blue〃)。OnAction = 〃ColorBlue〃
ntrols(〃Black〃)。OnAction = 〃ColorBlack〃
End With
End Sub
Sub ColorRed()
ActiveCell。Fontlor = RGB(255; 0; 0)
End Sub
Sub ColorGreen()
ActiveCell。Fontlor = RGB(0; 255; 0)
End Sub
Sub ColorBlue()
ActiveCell。Fontlor = RGB(0; 0; 255)
End Sub
Sub ColorBlack()
ActiveCell。Fontlor = RGB(0; 0; 0)
End Sub
11。修改内置快捷菜单
Excel提供了60来个快捷菜单,带有不同的经常用到的菜单项。当你在Excel应用程序窗口的某个对
象上单击右键时,快捷菜单就会出现。通过使用VBA,你可以返回快捷菜单的准确编号,还有它们
的名称。
1。 在当前工程的模块里输入过程ShortcutMenus,如下所示:
Sub ShortcutMenus()
Dim myBar As mandBar
Dim counter As Integer
For Each myBar In mandBars
If myBar。Type = msoBarTypePopup Then
counter = counter + 1
Debug。Print counter & 〃: 〃 & myBar。Name
End If
Next
End Sub
注意,要使用常量msoBarTypePopup来确定mandBars集合里的快捷菜单类型。使用常量
msoBarTypeMenuBar,可以返回内置菜单的名称。
当你运行过程ShortcutMenus后,快捷菜单的名称就会打印在立即窗口里,这里也列出来了。
Excel 2002 的内置快捷菜单: Excel 2003 的内置快捷菜单:
1: Query and Pivot 1: Query and Pivot
2: PivotChart Menu 2: PivotChart Menu
3: Workbook tabs 3: Workbook tabs
244
… 页面 261…
4: Cell 4: Cell
5: Column 5: Column
6: Row 6: Row
7: Cell 7: Cell
8: Column 8: Column
9: Row 9: Row
10: Ply 10: Ply
11: XLM Cell 11: XLM Cell
12: Document 12: Document
13: Desktop 13: Desktop
14: Nondefault Drag and Drop 14: Nondefault Drag and Drop
15: AutoFill 15: AutoFill
16: Button 16: Button
17: Dialog 17: Dialog
18: Series 18: Series
19: Plot Area 19: Plot Area
20: Floor and Walls 20: Floor and Walls
21: Trendline 21: Trendline
22: Chart 22: Chart
23: Format Data Series 23: Format Data Series
24: Format Axis 24: Format Axis
25: Format Legend Entry 25: Format Legend Entry
26: Formula Bar 26: Formula Bar
27: PivotTable Context Menu 27: PivotTable Context Menu
28: Query 28: Query
29: Query Layout 29: Query Layout
30: AutoCalculate 30: AutoCalculate
31: Object/Plot 31: Object/Plot
32: Title Bar (Charting) 32: Title Bar (Charting)
33: Layout 33: Layout
34: Pivot Chart Popup 34: Pivot Chart Popup
35: Phonetic Information 35: Phonetic Information
36: Auto Sum 36: Auto Sum
37: Paste Special Dropdown 37: Paste Special Dropdown
38: Find Format 38: Find Format
39: Replace Format 39: Replace Format
40: Shapes 40: Shapes
41: Inactive Chart 41: Inactive Chart
42: Excel Control 42: Excel Control
43: Curve 43: Curve
44: Curve Node 44: Curve Node
45: Curve Segment 45: Curve Segment
46: Pictures Context Menu 46: Pictures Context Menu
47: OLE Object 47: OLE Object
48: ActiveX Control 48: ActiveX Control
49: WordArt Context Menu 49: WordArt Context Menu
50: Rotate Mode 50: Rotate Mode
51: Connector 51: Connector
52: Script Anchor Popup 52: Script Anchor Popup
245
… 页面 262…
53: Canvas Popup 53: Canvas Popup
54: Organization Chart Popup 54: Organization Chart Popup
55: Diagram 55: Diagram
56: Add mand 56: Layout
57: Built…in Menus 57: Select
58: System 58: List Range Popup
59: Layout 59: List Range Layout Popup
60: Select 60: XML Range Popup
61: List Range Layout Popup
62: Built…in Menus
现在,你已经知道里Excel快捷菜单的准确名称了,你可以轻易地添加其它经常用到的命令到这些
菜单中的任意菜单中去。尽管从工具栏上点击打印图标或者选择文件|打印来打印工作表都是很容
易的事情,但是你可能还是想将该打印命令添加到快捷菜单中去,当你在工作表标签上单击右键时
就会出现在该快捷菜单上。我们来看看如何添加该选项到Ply菜单上去。
2。 输入如下所示地过程AddToPlyMenu:
Sub AddToPlyMenu()
With ApplicationmandBars(〃Ply〃)
。Reset
ntrols。Add(Type:=msoControlButton; Before:=2)。Caption = _
〃Print。。。〃
ntrols(〃Print。。。〃)。OnAction = 〃PrintSheet〃
End With
End Sub
上面所用地Reset方法避免当你多次运行该过程时,将同样的选项放置到该快捷菜单上。
3。 运行过程AddToPlyMenu,然后返回到代码窗口,并且输入下述过程,当你从该快捷菜单上选择
Print选项时,就会执行该过程:
Sub PrintSheet()
Application。Dialogs(xlDialogPrint)。Show
End Sub
4。 切换到Excel应用程序窗口,并且在任何工作表标签上单击右键,选择Print选项,你应该可以
看到当你使用其它内置工具打印时看到的相同的对话框。
图12…10 自定义选项可以添加到内置快捷菜单上(参见Print选项在过程AddToPlyMenu里被添加上
了)
246
… 页面 263…
12。创建快捷菜单
1。 在当前VBA工程的代码窗口里输入过程Create_ShortMenu,如下所示:
Sub Create_ShortMenu()
Dim sm As Object
Set sm = ApplicationmandBars。Add(〃Information〃; msoBarPopup)
With sm
ntrols。Add(Type:=msoControlButton)。Caption = 〃Operating System〃
With ntrols(〃Operating System〃)
。FaceId = 1954
。OnAction = 〃OpSystem〃
End With
ntrols。Add(Type:=msoControlButton)。Caption = 〃Total Memory〃
With ntrols(〃Total Memory〃)
。FaceId = 1977
。OnAction = 〃TotalMemory〃
End With
ntrols。Add(Type:=msoControlButton)。Caption = 〃Used Memory〃
With ntrols(〃Used Memory〃)
。FaceId = 2081
。OnAction = 〃UsedMemory〃
End With
ntrols。Add(Type:=msoControlButton)。Caption = 〃Free Memory〃
With ntrols(〃