Note: The other languages of the website are Google-translated. Back to English
English English

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

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.

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

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!
officetab bottom
Comments (2)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hello, This did not work for me:( All my macros have names, is that the problem?Thanks for any help
This comment was minimized by the moderator on the site
can this be altered to just export all Moduls from the personal.XLSB? Just for regular "Backup of my Macros" - Reasons
There are no comments posted here yet
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations