新浪博客

VBA练习—增加右键自定义菜单和增加菜单

2011-04-22 19:11阅读:
Private Sub Workbook_Open()
Call Addright '工作薄打开时调用添加右键菜单
End Sub
Sub Addright() '在模块中增加此过程
Dim Addpop As CommandBarPopup, Addbutton As CommandBarButton
On Error Resume Next '出现错误继续
Application.CommandBars('cell').Controls('自定义(&M)').Delete '删除右键上的自定义菜单,否则每一次打开都增加一次,当cell变为1,则为增加菜单(与文件并列的菜单)
Set Addpop = Application.CommandBars('cell').Controls.Add(Type:=msoControlPopup, before:=1) '增加一级菜单popup
Addpop.Caption = '自定义(&M)' '菜单名称为自定义,当cell变为1,则为增加菜单下的按钮(与文件并列的菜单)
Set Addbutton = Addpop.Controls.Add(Type:=msoControlButton, before:=1) '增加二级菜单按钮'可以增加多个
With Addbutton
.Caption = '插入作者' '菜单名称为插入作者
.FaceId = 30 '按钮图标
.OnAction = 'InsertAuthor' '按钮执行动作,调用InsertAuthor函数
End With
End Sub
Sub InsertAuthor() '在模块中增加此过

Selection = ThisWorkbook.Author '选中的单元格赋值为本文档作者
End Sub
VBA练习—增加右键自定义菜单和增加菜单
VBA练习—增加右键自定义菜单和增加菜单
__________________________________________________________________
Application.CommandBars('ply').Enabled = aa'aa代表true,下同
'右键点工作表标签是否可用
Application.CommandBars('cell').Enabled = aa
'右键点单元格是否可用
Application.CommandBars('toolbar list').Enabled = aa
'右键点工具栏及“视图”-“工具栏”是否可用
Application.CommandBars('autocalculate').Enabled = aa
'右键点状态栏是否可用
Application.CommandBars('worksheet menu bar').Enabled = aa
Application.CommandBars(1).Enabled = aa
'以上两句的作用都是把菜单取消
Application.CommandBars(2).Enabled = aa
'暂时未知
Application.CommandBars(3).Enabled = aa
'常用工具栏是否可用
Application.CommandBars(3).Controls(3).Enabled = aa
'使常用工具栏中第三个“保存”是否可用
Application.CommandBars(4).Enabled = aa
'格式工具栏是否可用
Application.CommandBars(5).Enabled = aa
'暂时未知
Application.CommandBars(1).Enabled = aa
'恢复菜单可用
Application.CommandBars(1).Controls(1).Enabled = aa
'使菜单中的“文件”是否可用
Application.CommandBars('file').Controls('页面设置(&U)...').Enabled = aa
'菜单中的“文件”中的“页面设置”是否可用
Application.CommandBars(1).Controls(4).Enabled = aa
'菜单中的第四个“插入”是否可用
Application.CommandBars(1).Controls(4).Caption = 'victor'
'更改名称
Application.CommandBars(1).Reset
'恢复菜单
Application.DisplayFormulaBar = aa
'编辑栏是否显示
Application.DisplayStatusBar = aa
'状态栏是否显示
ActiveWindow.DisplayHeadings = aa
'行号列标是否显示
ActiveWindow.DisplayWorkbookTabs = aa
'工作表标签是否显示
ActiveWindow.DisplayHorizontalScrollBar = aa
'水平滚动条是否显示
ActiveWindow.DisplayVerticalScrollBar = aa
'垂直滚动条是否显示

我的更多文章

下载客户端阅读体验更佳

APP专享