vba - 如何以编程方式添加 Excel 2010 宏

标签 vba excel excel-2010

是否有任何方法可以通过编程方式将宏添加到 Excel 文件?
我有太多 Excel 文件,我想向其中添加宏。 手动添加(手动)似乎是不可能的。 我需要创建一个工具来执行此操作。

最佳答案

是的,您可以通过编程方式完成此操作,您可以通过代码访问VB集成开发环境。以下网站非常适合学习 VBIDE,我已使用它们来编写此代码。如果运行 WorkbookModuleImport,将弹出两个打开的对话框,第一个对话框询问要导入模块的工作簿,第二个对话框选择要导入的模块。

Sub WorkbookModuleImport()
    Dim ii As Integer, vFileNames As Variant, vModules As Variant

    'We'll use the Application.GetOpenFilename to get a list of all the excel     workbooks we want to import into
    vFileNames = Application.GetOpenFilename(",*.xls;*.xlsx;*.xlsm", , "Select Workbooks to Import Modules To", , True)
    'If the result is not an array it means the cancel button has been pressed
    If Not IsArray(vFileNames) Then Exit Sub

    'Use the same method to get all the modules/classes/forms to input
    vModules = Application.GetOpenFilename(",*.cls, *.bas, *.frm", , "Select Modules/Forms/Class Modules to Import", , True)
    If Not IsArray(vModules) Then Exit Sub

    'Now loop through all the workbooks to import the modules
    For ii = LBound(vFileNames) To UBound(vFileNames)
        Call ImportModules(VBA.CStr(vFileNames(ii)), vModules)
    Next ii

End Sub



Public Sub ImportModules(sWorkbookName As String, vModules As Variant)
    Dim cmpComponents As VBIDE.VBComponents, ii As Integer
    Dim wkbTarget As Excel.Workbook

    'We need to open the workbook in order to be able to import the code module
     Set wkbTarget = Workbooks.Open(sWorkbookName)

    'If the project is protected with a password we can't import so just set tell us in the immediate window
    If wkbTarget.VBProject.Protection = 1 Then
        'Give a message
        Debug.Print wkbTarget.Name & " has a protected project, cannot import module"
        GoTo Cancelline
    End If

    'This is where we set the reference to the components of the Visual Basic project
    Set cmpComponents = wkbTarget.VBProject.VBComponents

   'Loop through all the modules to import
    For ii = LBound(vModules) To UBound(vModules)
        cmpComponents.Import vModules(ii)
    Next ii


Cancelline:
   'If it's in Excel 2007+ format but doesn't already have macros, we'll have to save it as a macro workbook
   If wkbTarget.FileFormat = xlOpenXMLWorkbook Then
       wkbTarget.SaveAs wkbTarget.Name, xlOpenXMLWorkbookMacroEnabled
       wkbTarget.Close SaveChanges:=False
   Else
       'Otherwise, just save the workbook and close it
        wkbTarget.Close SaveChanges:=True
   End If

   'I don't trust excel, so set the workbook object to nothing
   Set wkbTarget = Nothing
End Sub

这些网页是很好的引用: http://www.cpearson.com/excel/vbe.aspxhttp://www.rondebruin.nl/vbaimportexport.htm 。我以罗恩的为起点。

关于vba - 如何以编程方式添加 Excel 2010 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14493473/

相关文章:

vba - 如何使用宏 vba 从特定列中删除重复行

excel - 基于一列值的 Excel VBA 过滤表

vba - 使用 Lotus Notes Social Edition 从 Excel 数据生成电子邮件

vba - 删除用户表单中动态创建的文本框的问题

excel - 访问共享网络文件夹

html - 在 Excel 中打开文件时,使用 XSL 创建的空 html 列太宽

Excel公式比较两个数组

Excel VBA 添加自动筛选器(如果不存在)

vba - Excel VBA : Date Comparison

mysql - 将 Excel 表导入 MySQL 阿拉伯语数据