有没有办法编写一个 VBA 宏来将另一个 VBA 宏输入到多个 excel 工作簿中?如果是这样,我该如何开始?
非常感谢任何和所有帮助。
你首先需要一个参考
Microsoft Visual Basic For Applications Extensibility 5.3
给你。玩得开心
Public Sub AddNewModule()
Dim proj As VBIDE.VBProject
Dim comp As VBIDE.VBComponent
Set proj = ActiveWorkbook.VBProject
Set comp = proj.VBComponents.Add(vbext_ct_StdModule)
comp.Name = "MyNewModule"
Set codeMod = comp.CodeModule
With codeMod
lineNum = .CountOfLines + 1
.InsertLines lineNum, "Public Sub ANewSub()"
lineNum = lineNum + 1
.InsertLines lineNum, " MsgBox " & """" & "I added a module!" & """"
lineNum = lineNum + 1
.InsertLines lineNum, "End Sub"
End With
End Sub
您也可以只使用其中包含代码的工作簿作为参考。然后就可以远程调用模块了。
正如@BruceWayne 提到的,在个人书中也有它。
tl; 博士 - 有几个选项可以让你到达那里。
我认为在略有不同的 Excel 文件中使用相同代码的最简单方法是拥有一个“模板”并将其多次保存为几个略有不同的文件。或者,如果您想变得花哨,您可以创建一个 AddIn 以使 Excel 宏可用于所有工作簿。
Option Explicit
Dim cControl As CommandBarButton
Private Sub Workbook_AddinInstall()
On Error Resume Next 'Just in case
'Delete any existing menu item that may have been left.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
'Add the new menu item and Set a CommandBarButton Variable to it
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
'Work with the Variable
With cControl
.Caption = "Super Code"
.Style = msoButtonCaption
.OnAction = "MyGreatMacro"
'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_AddinUninstall()
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
On Error GoTo 0
End Sub
只要用户通过“工具”>“加载项”安装了加载项,您就需要将单个菜单项(称为超级代码)添加到现有工作表菜单栏的末尾。单击 Super Code 菜单项时,将运行一个宏(即在加载项的标准模块中)。如前所述,上述代码必须放在 Add-in 的 ThisWorkbook 的 Private Module 中。
如果你想添加 Super Code 菜单项,比如在 Format 菜单项之前,你可以使用这样的代码。
Option Explicit
Dim cControl As CommandBarButton
Private Sub Workbook_AddinInstall()
Dim iContIndex As Integer
On Error Resume Next 'Just in case
'Delete any existing menu item that may have been left
Application.CommandBars("Worksheet Menu Bar").Controls("SuperCode").Delete
'Pass the Index of the "Format" menu item number to a Variable.
'Use the FindControl Method to find it's Index number. ID number _
is used in case of Customization
iContIndex = Application.CommandBars.FindControl(ID:=30006).Index
'Add the new menu item and Set a CommandBarButton Variable to it.
'Use the number passed to our Integer Variable to position it.
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add(Before:=iContIndex)
'Work with the Variable
With cControl
.Caption = "Super Code"
.Style = msoButtonCaption
.OnAction = "MyGreatMacro"
'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
在这种情况下,无需更改 Workbook_AddinUninstall() 代码。
我们在以前的时事通讯问题中介绍了使用 CommandBars 等时的 ID 号 可以在此处找到指向 Microsoft 站点的链接,该站点包含使用 CommandBars 的所有 ID 号的大列表
上面的例子实际上在 Workbook_AddinInstall 和 Workbook_AddinUnInstall 中都有所有的菜单项代码,当代码只添加一个菜单项时没有问题。但是,如果您要添加多个甚至可能是子菜单,则应将其放置在标准模块内的过程(或 2)中。然后使用如下所示的一些代码
Private Sub Workbook_AddinInstall()
Run "AddMenus"
End Sub
Private Sub Workbook_AddinUninstall()
Run "DeleteMenu"
End Sub
然后在标准模块中放一些代码可能是这样的
Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
'(1)Delete any existing one.We must use On Error Resume next _
in case it does not exist.
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&NewMenu").Delete
'(2)Set a CommandBar variable to Worksheet menu bar
Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
'(3)Return the Index number of the Help menu. We can then use _
this to place a custom menu before.
iHelpMenu = cbMainMenuBar.Controls("Help").Index
'(4)Add a Control to the "Worksheet Menu Bar" before Help
'Set a CommandBarControl variable to it
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu)
'(5)Give the control a caption
cbcCutomMenu.Caption = "&New Menu"
'(6)Working with our new Control, add a sub control and _
give it a Caption and tell it which macro to run (OnAction).
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Menu 1"
.OnAction = "MyMacro1"
End With
'(6a)Add another sub control give it a Caption _
and tell it which macro to run (OnAction)
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Menu 2"
.OnAction = "MyMacro2"
End With
'Repeat step "6a" for each menu item you want to add.
'Add another menu that will lead off to another menu
'Set a CommandBarControl variable to it
Set cbcCutomMenu = cbcCutomMenu.Controls.Add(Type:=msoControlPopup)
' Give the control a caption
cbcCutomMenu.Caption = "Next Menu"
'Add a control to the sub menu, just created above
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "&Charts"
.FaceId = 420
.OnAction = "MyMacro2"
End With
On Error GoTo 0
End Sub
Sub DeleteMenu()
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&NewMenu").Delete
On Error GoTo 0
End Sub
您可以在此处找到所有详细信息。
我建议将它们存储在Personal.xslb可通过 Excel 访问的文件中。
有关更多详细信息,请参阅此页面或此页面,但通常快速入门的方法是:
注意:如果您没有“Personal.xlsb”,则必须创建它。只需录制一个新宏,但选择将其存储在“个人宏工作簿”中。然后你应该在 VBEditor 中看到它。