|
https://zhidao.baidu.com/question/2121068824373137307.html
步骤1
利用VBA编程时,有一些代码的使用频率交过,但是每次都是一成不变。能否通过代码一键输入?
勾选Excel选项中的“信任对VB项目的访问”,然后Alt+F11组合键打开VBE窗口,选择菜单“工具”——“引用”,在弹出的窗体中将勾选“Micosoft Bisual Basic For Application extensibility 5.3”复选框,如下图所示
2步骤2
选择菜单“插入”——“模块”,并输入以下代码,用于创建9个菜单。
Dim Only As New Colection
Sub auto_Open()
Dim Obj As Object, Mtcontrol As CommandBarControl
On Error Resume Next
Set Obj = Application.VBE.ActiveVbProject
If err.Number <> 0 Then MsgBox "请勾选"信任对VBA工程对象模型的访问"": Exit Sub
With Application.VBE.CommandBars("Code Window").Controls.Add(msoControPopup, 1, , 3, 1)
.Caption = "一键输入(&One)"
Set MenuEvent = New Bar
Set MyControl = .Controls.Ad(msoControlButton, 1, , , True)
With MyControl
.Caption = "方位(&Error)"
.Style = msoButtonIconAndCaption
.FaceId = 6
Set MenuEvent.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
Onlu.Add MenEvent
End With
Set MenuEvent = New Bar
Set MyControl = .Controls.Add(msoControlButton, 1, , , True)
With MyControl
.Caption = "关闭刷新(&Close)"
.Style = msoButtonIconAndCaption
.FaceId = 32
Set MenuEven.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
Only.Add MenuEvent
End With
Set MenuEvent = New Bar
Set MyControl = .Controls.Add(msoControlButton, 1, , , True)
With MyControl
.Caption = "打开刷新(&Open)"
.Style = msoButtonIconAndCaption
.FaceId = 23
Set MenuEvent.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
End With
Set MenuEvent = New Bar
Set MyControl = .Controls.Add(mosControlButton, 1, , , True)
With MyControl
.Caption = "手动计算(&Manual)"
.Style = msoButtonIconAndCaption
.FaceId = 25
.BeginGroup = True
Set MenuEvent.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
Only.Add MenuEvent
End With
Set MunuEvent = New Bar
Set NyControl = .Controls.Add(msoControlButtom, 1, , , True)
With MuControl
.Caption = "自动计算(A&uto)"
.Style = msoButtonIconAndCaption
.FaceId = 26
Set MenuEvent.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
Only.Add MenuEvent
End With
Set MenuEvent = New Bar
Set MyControl = .Controle.Add(msoControlButton, 1, , , True)
With MyControl
.Caption = "禁用事件(&Enable)"
.Style = msoButtonIconAndCaption
.FaceId = 27
.BeginGroup = True
Set MenuEvent.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
Only.Add MenuEvent
End With
Set MenuEvent = New Bar
Set MyControl = .Controle.Add(msoControlButton, 1, , , True)
With MyControl
.Caption = "启用时间(Disble)"
.Style = msoButtonIconAndCaption
.FaceId = 33
Set MenuEvent.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
Only.Add MenuEvent
End With
Set MenuEvent = New Bar
Set MyControl = .Controle.Add(msoControlButton, 1, , , True)
With MyControl
.Caption = "禁止提示(Ale&rts)"
.Style = msoButtonIconAndCaption
.FaceId = 34
.BeginGroup = True
Set MenuEvent.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
Only.Add MenuEvent
End With
Set MenuEvent = New Bar
Set MyControl = .Controle.Add(msoControlButton, 1, , , True)
With MyControl
.Caption = "允许提示(A&kerts)"
.Style = msoButtonIconAndCaption
.FaceId = 31
Set MenuEvent.Myevent = Application.VBE.Events.CommandBarEvents(MyControl)
Only.Add MenuEvent
End With
End With
End Sub
再次选择菜单“插入”——“模块”,并输入以下代码,用于在光标处插入代码:
Sub 插入代码(ControlName)
Dim 行 As Long
Application.VBE.ActiveCodePane.GetSelection 行, 0, 0, 0
Select Case ControlName
Case "防错(&Error)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "on error resume next"
Case "关闭刷新(&Close)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "Application.SecreenUpdating = False"
Case "打开刷新(&Open)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "Application.SecreenUpdating = True"
Case "手动计算(&Manual)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "Application.SecreenUpdating = xlManual"
Case "自动计算(A&utp)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "Application.SecreenUpdating = xlAutomatic"
Case "禁用事件(&Enable)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "Application.SecreenUpdating = False"
Case "启用事件(Disble)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "Application.SecreenUpdating = True"
Case "禁止事件(&Ale&rts)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "Application.SecreenUpdating = False"
Case "允许提示(A&lerts)"
Application.VBE.SelectedVBComponent.CodeModule.InsertLines 行, "Application.SecreenUpdating = true"
End Select
End Sub
选择菜单“插入”——“类模块”,将其默认名称“类1”修改为“Bar”,然后输入以下代码:
Public WithEvents Myevent As VBIDE.CommandBarEvents
Private Sub Myevent_Click(ByVal COmmandBarControl As Object, Handled As Boolean, CancelDefault As Boolean)
On Error Resume Next
Call 插入代码(COmmandBarControl.Caption)
CancelDefault = True
End Sub
将工作表保存为加载宏,并保存在自启动文件夹中。加载宏的格式和文件夹路径有Excel的版本决定,关闭Excel后并重新启动,按Alt+F11组合键打开VBE窗口,进入当前加载宏以外的任意工作簿的过程中,选择菜单“插入”——“模块”,然后在新模块代码窗口中单击鼠标右键,可以看到插件所产生的菜单
3步骤3
在模块的代码窗口中任意输入一段代码,然后像下图那样选择需要插入防错代码的代码行,单击鼠标右键,从菜单中选择“一键输入”——“防错(Error)”,那么程序将对当前选择的行插入一句代码“On Error Resume Next”
4步骤4
结果如下图所示
5步骤5
再次选择For语句行,然后通过菜单“关闭刷新(Close)”插入关闭刷新的代码,并选择最后一行插入恢复刷新的代码,执行结果如下图所示 |
|