title: Copy all VBA codes from workbook to another
body: <p>I have searched a lot and found a lot of VBA codes that exports the VBA codes modules but what I need is different a little bit.
  I have a large project with a lot of VBA codes in standard modules, Worksheets modules, ThisWorkbook module. All of these have VBA codes and there is another workbook say ("New.xlsm") which I need to copy all these VBA codes to it.
  But before exporting those VBA codes, I need to clean the "New.xlsm" from any codes at any module at all or delete any existing module and clean everything ..then copy the vba codes to the "New.xlsm".</p>
  
  <p>I have this code that exports all VBE components but this may be a step only.</p>
  
  <pre><code>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 &amp; "\" &amp; ActiveWorkbook.name &amp; " Modules"
      If Dir(destDir, vbDirectory) = vbNullString Then MkDir destDir
  
      For Each vbComp In ActiveWorkbook.VBProject.VBComponents
          If vbComp.CodeModule.CountOfLines &gt; 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 &lt;&gt; vbNullString Then
                  fName = destDir &amp; "\" &amp; vbComp.name &amp; ext
                  If Dir(fName, vbNormal) &lt;&gt; vbNullString Then Kill (fName)
                  vbComp.Export (fName)
              End If
          End If
      Next vbComp
  End Sub
  </code></pre>
  
  <p>I have solved the first step which will removes all the existing codes from "original.xlm"</p>
  
  <pre><code>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
  </code></pre>
  
  <p>What I need now is to copy all the macros from the "New.xlm" to "Original.xlsm"</p>
  
  <p>I found this code but this require to name each module that I need to copy. I don't need to specify any module name as I have about 30 modules and also worksheets modules ..and also ThisWorkbook module</p>
  
  <pre><code>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 &amp; "\" &amp; varModule &amp; ".bas"
                      wbkSource.VBProject.VBComponents(varModule).Export Filename:=strModule
                      On Error Resume Next
                          .Remove VBComponent:=.Item(varModule)
                      On Error GoTo 0
                      .Import Filename:=ThisWorkbook.Path &amp; "\" &amp; varModule &amp; ".bas"
                      Kill strModule
                  Next varModule
              End With
      Application.EnableEvents = True
      Application.ScreenUpdating = True
  End Sub
  </code></pre>
  
  <p>*** To Copy worksheets modules I have found this</p>
  
  <pre><code>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
  </code></pre>