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:
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. 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:
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:
Note: This code can only export the macros from normal Module.
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.
Best Office Productivity Tools
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!