Skip to main content
 

How to export all macros from one workbook to another workbook?

Author: Xiaoyang Last Modified: 2024-09-29

If you need to export multiple macro vba codes from one workbook to another, you can copy them one by one normally. But this will be time-consuming, this article, I will talk about how to quickly export all macros from one workbook to another and save the macro files to a specific folder as well.

Export all macros from one workbook to another with VBA code


Export all macros from one workbook to another with VBA code

To export all macro vba codes from one Excel file to another, please do with following steps:

1. First, you should open the two workbooks at the same time, one contains macros, the another is the one you want to export macros to, see screenshot:

open the two workbooks at the same time

Office Tab-Open, read, edit, and manage multiple Office documents in a single tabbed window

Office Tab enables tabbed browsing, editing, and managing of Microsoft Office applications. You can open multiple documents / files in a single tabbed window, such as using the browser IE 8/9/10, Firefox, and Google Chrome. Click to download and Free trial now!

2. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

3. Click Insert > Module, and paste the following macro in the Module Window.

VBA code: Export all macros from one workbook to another:

Sub ExportAndImportModule()
Dim xStrSWSName, xSreDWSName As String
Dim xSWS, xDWS As Workbook
xStrSWSName = "old-workbook"
xSreDWSName = "new-workbook"
Dim xFilePath As String
Dim xObjFD As FileDialog
Set xObjFD = Application.FileDialog(msoFileDialogFolderPicker)
 With xObjFD
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count > 0 Then
            xFilePath = .SelectedItems.Item(1)
        Else
            Exit Sub
        End If
End With
On Error GoTo Err1
Set xSWS = Workbooks(xStrSWSName & ".xlsm")
Set xDWS = Workbooks(xSreDWSName & ".xlsm")
Set xvbap = xSWS.VBProject
Set xVBC = xvbap.VBComponents
For Each Module In xSWS.VBProject.VBComponents
If Module.Type = vbext_ct_StdModule Then
Module.Export (xFilePath & "\" & Module.Name & ".bas")
xDWS.VBProject.VBComponents.Import (xFilePath & "\" & Module.Name & ".bas")
End If
Next Module
Exit Sub
Err1:
    MsgBox "come to nothing!"
End Sub
Note: In the above code, “old-workbook” is the name of workbook which you want to export the macros from, and “new-workbook” is the workbook that you want to import the macros. You should change the names to your own. Both of the two workbooks should be xlsm file format.

copy and paste the code into the module

4. After pasting the above code, then press F5 key to run this code, and a Browse window is displayed, please choose a folder to put the exported macro files, see screenshot:

run vba code to choose a folder to put the exported macro files

5. Then click OK button, and all macros have been exported from your specified workbook to another Excel file, and the macro files have been saved into the specific folder as well. See screenshot:

all macros have been exported from your specified workbook to another Excel file

Note: This code can only export the macros from normal Module.

a screenshot of kutools for excel ai

Unlock Excel Magic with Kutools AI

  • Smart Execution: Perform cell operations, analyze data, and create charts—all driven by simple commands.
  • Custom Formulas: Generate tailored formulas to streamline your workflows.
  • VBA Coding: Write and implement VBA code effortlessly.
  • Formula Interpretation: Understand complex formulas with ease.
  • Text Translation: Break language barriers within your spreadsheets.
Enhance your Excel capabilities with AI-powered tools. Download Now and experience efficiency like never before!

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!