Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in


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

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

Remove all macros from multiple workbooks:

Kutools for Excel's Batch Remove All Macros utility can help you to remove all macros from multiple workbooks as you need. Download and free trial Kutools for Excel now!

doc run macro if cell changes 3

Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!

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:

doc export macros 1

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!

doc export macros

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
        If .SelectedItems.Count > 0 Then
            xFilePath = .SelectedItems.Item(1)
            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
    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.

doc export macros 2

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:

doc export macros 3

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:

doc export macros 4

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

Recommended Productivity Tools for Excel

kte tab 201905

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: 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!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.