如何使用VBA将宏添加到多个excel文件

如何使用VBA将宏添加到多个excel文件,vba,excel,Vba,Excel,有没有办法编写一个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

有没有办法编写一个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访问的
Personal.xslb
文件中

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

  • 按ALT+F11打开VBEditor
  • 右键单击“VBA项目(PERSONAL.XLSB)”并添加新模块
  • 在模块中添加代码
  • 现在,当您转到查看-->宏时,可以选择查看存储在Personal.xlsb文件中的宏:

  • (为了隐私,我将我的宏“涂白”,但它们将按名称列出)

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


    我认为在稍微不同的Excel文件中使用相同代码的最简单方法是使用一个“模板”,并将其保存为几个稍微不同的文件。或者,如果您想玩得开心,可以创建一个加载项,使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
    
    在这种情况下,无需更改工作簿\u AddInInstall()代码

    我们在一期期新闻稿中介绍了使用CommandBars时的ID号等。可以在此处找到指向Microsoft站点的链接,该站点有一个使用CommandBars的所有ID号的大列表

    上述示例实际上包含工作簿中的所有菜单项代码\u addin安装和工作簿\u addin安装,当代码仅添加一个菜单项时,这不成问题。但是,如果要添加一个以上甚至子菜单,则应将其放置在标准模块内的过程(或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.xlsb
    中是否不起作用?这些可以从任何Excel文件中访问。@BruceWayne我不熟悉你的建议。您可以详细说明吗?或者使用包含宏的外接程序。