如何使用VBA向多个Excel文件添加宏

时间:2018-07-11 14:36:00

标签: excel vba excel-vba

是否可以编写一个VBA宏以将另一个VBA宏输入到多个excel工作簿中?如果是这样,我该如何开始?

我们将不胜感激。

3 个答案:

答案 0 :(得分:1)

您首先需要参考

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; dr-有几种选择可以帮助您到达那里。

答案 1 :(得分:0)

我建议将它们存储在Personal.xslb文件中,该文件可通过Excel进行访问。

有关更多详细信息,请参见this pagethis page,但是通常一种快速的入门方法是:

  1. ALT + F11 打开VBEditor。
  2. 右键单击“ VBAProject(PERSONAL.XLSB)”,然后添加一个新模块
  3. 在模块中添加代码。
  4. 现在,当您转到查看->宏时,可以选择查看存储在Personal.xlsb文件中的那些宏:

enter image description here
(出于隐私考虑,我“清空了”我的宏,但它们会按名称列出)

注意:如果没有“ Personal.xlsb”,则必须创建它。只需记录一个新的宏,然后选择将其存储在“个人宏工作簿”中。然后,您应该在VBEditor中看到它。

enter image description here

答案 2 :(得分:0)

我认为在略有不同的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

一旦用户通过“工具”>“加载项”安装了加载项,此代码便是将单个菜单项(称为“超级代码”)添加到现有工作表菜单栏末尾所需的全部代码。单击“超级代码”菜单项时,将运行一个宏(在外接程序的标准模块内)。如前所述,必须将以上代码放置在ThisWorkbook的私有模块中作为外接程序。

如果要添加“超级代码”菜单项,例如在“格式”菜单项之前,则可以使用类似这样的代码。

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号的BIG列表。

以上示例实际上在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

您可以在此处找到所有详细信息。

http://www.ozgrid.com/VBA/excel-add-in-create.htm