将所有VBA代码从工作簿复制到另一个

问题描述 投票:0回答:2

我已经搜索了很多,发现了很多可以导出VBA代码模块的VBA代码,但是我需要的有所不同。我有一个大型项目,在标准模块,工作表模块,ThisWorkbook模块中有很多VBA代码。所有这些都有VBA代码,还有另一个工作簿说(“ New.xlsm”),我需要将所有这些VBA代码复制到其中。但是在导出这些VBA代码之前,我需要从所有模块上的所有代码中清除“ New.xlsm”,或者删除任何现有模块并清除所有内容..然后将vba代码复制到“ New.xlsm”。

我有导出所有VBE组件的代码,但这仅是一个步骤。

Sub Export_All_VBE_Components()
'References: Microsoft Visual Basic for Applications Extensibility 5.3
'---------------------------------------------------------------------
    Dim vbComp          As VBIDE.VBComponent
    Dim destDir         As String
    Dim fName           As String
    Dim ext             As String

    If ActiveWorkbook.Path = "" Then MsgBox "You Must First Save This Workbook Somewhere So That It Has A Path.", , "Error": Exit Sub
    destDir = ActiveWorkbook.Path & "\" & ActiveWorkbook.name & " Modules"
    If Dir(destDir, vbDirectory) = vbNullString Then MkDir destDir

    For Each vbComp In ActiveWorkbook.VBProject.VBComponents
        If vbComp.CodeModule.CountOfLines > 0 Then
            Select Case vbComp.Type
                Case vbext_ct_ClassModule: ext = ".cls"
                Case vbext_ct_Document: ext = ".cls"
                Case vbext_ct_StdModule: ext = ".bas"
                Case vbext_ct_MSForm: ext = ".frm"
                Case Else: ext = vbNullString
            End Select

            If ext <> vbNullString Then
                fName = destDir & "\" & vbComp.name & ext
                If Dir(fName, vbNormal) <> vbNullString Then Kill (fName)
                vbComp.Export (fName)
            End If
        End If
    Next vbComp
End Sub

我已经解决了将从“ original.xlm”中删除所有现有代码的第一步

Sub Test_RemoveAllMacros()
    Application.ScreenUpdating = False
        RemoveAllMacros Application.Workbooks("Original.xlsm")
    Application.ScreenUpdating = True
End Sub

Sub RemoveAllMacros(wbk As Workbook)
    Dim vbCode As Object, vbComp As Object, vbProj As Object
    Set vbProj = wbk.VBProject
    With vbProj
        For Each vbComp In .VBComponents
            Select Case vbComp.Type
                Case 1, 2, 3
                    vbProj.VBComponents.Remove vbComp
                Case 100
                    Set vbCode = vbComp.CodeModule
                    vbCode.DeleteLines 1, vbCode.CountOfLines
            End Select
        Next vbComp
    End With
End Sub

我现在需要将所有宏从“ New.xlm”复制到“ Original.xlsm”

我找到了此代码,但这需要命名我需要复制的每个模块。我不需要指定任何模块名称,因为我大约有30个模块,还有工作表模块..还有ThisWorkbook模块

Sub Copy_module()
    Dim varModule, wbkSource As Workbook, wbkTarget As Workbook, strModule As String
    Application.ScreenUpdating = False
    Application.EnableEvents = False
        Set wbkSource = ThisWorkbook
            Set wbkTarget = Application.Workbooks("Original.xlsm")
            With wbkTarget.VBProject.VBComponents
                For Each varModule In Array("Module1", "Module2")
                    strModule = ThisWorkbook.Path & "\" & varModule & ".bas"
                    wbkSource.VBProject.VBComponents(varModule).Export Filename:=strModule
                    On Error Resume Next
                        .Remove VBComponent:=.Item(varModule)
                    On Error GoTo 0
                    .Import Filename:=ThisWorkbook.Path & "\" & varModule & ".bas"
                    Kill strModule
                Next varModule
            End With
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

***要复制工作表模块,我发现了这个

Sub CopyWorksheetsModules()
    Dim src, dest, wb As Workbook, ws As Worksheet
    On Error Resume Next
        For Each ws In ThisWorkbook.Worksheets
            Set src = ThisWorkbook.VBProject.VBComponents(ws.CodeName).CodeModule
            Set wb = Workbooks("Original.xlsm")
            Set dest = wb.VBProject.VBComponents(ws.CodeName).CodeModule
            dest.DeleteLines 1, dest.CountOfLines
            dest.AddFromString src.Lines(1, src.CountOfLines)
        Next ws
    On Error GoTo 0
End Sub
excel vba
2个回答
1
投票

这是我用来导入/导出模块的代码。导出模块将删除保存它们的文件夹中的所有当前文件。并且导入模块将在导入之前删除所有模块。请注意:

ElseIf Not VBComp.Name Like "*Modulos*" Then
     VBProj.VBComponents.Remove VBComp
End If

是为了避免删除导入/导出模块。它们分别称为ImportarModulosExportarModulos,因此请使用关键字来标识它们,并避免将它们删除和导入(因为这可能会给您带来麻烦。)

导出模块:

Option Explicit
Public Sub ExportModules()
    Dim bExport As Boolean
    Dim wkbSource As Excel.Workbook
    Dim szSourceWorkbook As String
    Dim szExportPath As String
    Dim szFileName As String
    Dim cmpComponent As VBIDE.VBComponent

    ''' The code modules will be exported in a folder named.
    ''' VBAProjectFiles in the Documents folder.
    ''' The code below create this folder if it not exist
    ''' or delete all files in the folder if it exist.
    If FolderWithVBAProjectFiles = "Error" Then
        MsgBox "Export Folder not exist"
        Exit Sub
    End If

    On Error Resume Next
        Kill FolderWithVBAProjectFiles & "\*.*"
    On Error GoTo 0

    ''' NOTE: This workbook must be open in Excel.
    szSourceWorkbook = ActiveWorkbook.Name
    Set wkbSource = Application.Workbooks(szSourceWorkbook)

    If wkbSource.VBProject.Protection = 1 Then
    MsgBox "The VBA in this workbook is protected," & _
        "not possible to export the code"
    Exit Sub
    End If

    szExportPath = FolderWithVBAProjectFiles & "\"

    For Each cmpComponent In wkbSource.VBProject.VBComponents

        bExport = True
        szFileName = cmpComponent.Name

        ''' Concatenate the correct filename for export.
        Select Case cmpComponent.Type
            Case vbext_ct_ClassModule
                szFileName = szFileName & ".cls"
            Case vbext_ct_MSForm
                szFileName = szFileName & ".frm"
            Case vbext_ct_StdModule
                szFileName = szFileName & ".bas"
            Case vbext_ct_Document
                ''' This is a worksheet or workbook object.
                ''' Don't try to export.
                bExport = False
        End Select
        If bExport Then
            ''' Export the component to a text file.
            cmpComponent.Export szExportPath & szFileName
        ''' remove it from the project if you want
        '''wkbSource.VBProject.VBComponents.Remove cmpComponent
        End If
    Next cmpComponent

    Dim wb As Workbook, ws As Worksheet, LastRow As Long
    Set wb = Workbooks.Open("Z:\Planificacion-WFM\Planificacion Telefonica\Código\Log.xlsx")
    Set ws = wb.Sheets(1)
    LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
    ws.Cells(LastRow, 1) = Application.UserName
    ws.Cells(LastRow, 2) = Format(Now(), "hh:mm:ss")
    ws.Cells(LastRow, 3) = Format(Now(), "dd/mm/yyyy")
    wb.Close Savechanges:=True


    MsgBox "Export is ready"
End Sub
Function FolderWithVBAProjectFiles() As String
    Dim WshShell As Object
    Dim FSO As Object
    Dim SpecialPath As String

    Set WshShell = CreateObject("WScript.Shell")
    Set FSO = CreateObject("scripting.filesystemobject")

    SpecialPath = "Z:\Planificacion-WFM\Planificacion Telefonica\Código"

    If Right(SpecialPath, 1) <> "\" Then
        SpecialPath = SpecialPath & "\"
    End If

    If FSO.FolderExists(SpecialPath & "VBAProjectFiles") = False Then
        On Error Resume Next
        MkDir SpecialPath & "VBAProjectFiles"
        On Error GoTo 0
    End If

    If FSO.FolderExists(SpecialPath & "VBAProjectFiles") = True Then
        FolderWithVBAProjectFiles = SpecialPath & "VBAProjectFiles"
    Else
        FolderWithVBAProjectFiles = "Error"
    End If

End Function

导入模块:

Option Explicit
Public Sub ImportModules()
    Dim wkbTarget As Excel.Workbook
    Dim objFSO As Scripting.FileSystemObject
    Dim objFile As Scripting.File
    Dim szTargetWorkbook As String
    Dim szImportPath As String
    Dim szFileName As String
    Dim cmpComponents As VBIDE.VBComponents
'
'    If ActiveWorkbook.Name = ThisWorkbook.Name Then
'        MsgBox "Select another destination workbook" & _
'        "Not possible to import in this workbook "
'        Exit Sub
'    End If

    'Get the path to the folder with modules
    If FolderWithVBAProjectFiles = "Error" Then
        MsgBox "Import Folder not exist"
        Exit Sub
    End If

    ''' NOTE: This workbook must be open in Excel.
    szTargetWorkbook = ActiveWorkbook.Name
    Set wkbTarget = Application.Workbooks(szTargetWorkbook)

    If wkbTarget.VBProject.Protection = 1 Then
    MsgBox "The VBA in this workbook is protected," & _
        "not possible to Import the code"
    Exit Sub
    End If

    ''' NOTE: Path where the code modules are located.
    szImportPath = FolderWithVBAProjectFiles & "\"

    Set objFSO = New Scripting.FileSystemObject
    If objFSO.GetFolder(szImportPath).Files.Count = 0 Then
       MsgBox "There are no files to import"
       Exit Sub
    End If

    'Delete all modules/Userforms from the ActiveWorkbook
    Call DeleteVBAModulesAndUserForms

    Set cmpComponents = wkbTarget.VBProject.VBComponents

    ''' Import all the code modules in the specified path
    ''' to the ActiveWorkbook.
    For Each objFile In objFSO.GetFolder(szImportPath).Files
        If objFile.Name Like "*Modulos*" Then GoTo Siguiente
        If (objFSO.GetExtensionName(objFile.Name) = "cls") Or _
            (objFSO.GetExtensionName(objFile.Name) = "frm") Or _
            (objFSO.GetExtensionName(objFile.Name) = "bas") Then
            cmpComponents.Import objFile.Path
        End If
Siguiente:
    Next objFile

    MsgBox "Módulos actualizados"
End Sub

Function DeleteVBAModulesAndUserForms()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent

        Set VBProj = ActiveWorkbook.VBProject

        For Each VBComp In VBProj.VBComponents
            If VBComp.Type = vbext_ct_Document Then
                'Thisworkbook or worksheet module
                'We do nothing
            ElseIf Not VBComp.Name Like "*Modulos*" Then
                VBProj.VBComponents.Remove VBComp
            End If
        Next VBComp
End Function

1
投票

我尚未测试此代码,但这是我发现的内容:

将模块从一个工作簿复制到另一个[credit]

Sub CopyModule(SourceWB As Workbook, strModuleName As String, _
    TargetWB As Workbook)
' 
' example: 
' CopyModule Workbooks("Book1.xls"), "Module1", _
    Workbooks("Book2.xls")
Dim strFolder As String, strTempFile As String
    strFolder = SourceWB.Path
    If Len(strFolder) = 0 Then strFolder = CurDir
    strFolder = strFolder & "\"
    strTempFile = strFolder & "~tmpexport.bas"
    On Error Resume Next
    SourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
    TargetWB.VBProject.VBComponents.Import strTempFile
    Kill strTempFile
    On Error GoTo 0
End Sub

要从工作簿中删除所有代码[credit]

Sub DeleteAllCode() 

     'Trust Access To Visual Basics Project must be enabled.
     'From Excel: Tools | Macro | Security | Trusted Sources

    Dim x               As Integer 

    On Error Resume Next 
    With Workbooks("Wb").VBProject 
        For x = .VBComponents.Count To 1 Step -1 
            .VBComponents.Remove .VBComponents(x) 
        Next x 
        For x = .VBComponents.Count To 1 Step -1 
            .VBComponents(x).CodeModule.DeleteLines _ 
            1, .VBComponents(x).CodeModule.CountOfLines 
        Next x 
    End With 
    On Error GoTo 0 

End Sub 

希望有所帮助。

热门问题
推荐问题
最新问题