You can export every VBA module in an Excel workbook in two ways: right-click each module in the Visual Basic Editor and choose Export File for a handful of modules, or run a short macro that loops through every component in the project and writes each one to disk as a .bas, .cls or .frm file. The macro approach is the only practical option once you have more than a few modules, because it exports everything in one pass and keeps the file extensions correct. Nothing needs to be installed first, but you do need the Developer tab switched on and the workbook saved as an .xlsm or .xlsb file, since a standard .xlsx cannot store macros at all.
What You'll Need
- A macro-enabled workbook (.xlsm or .xlsb) that actually contains VBA code. If the code lives in a .xls or .xla legacy file, open it in Excel and save a copy in the newer format first.
- The Developer tab visible. If it is missing, go to File > Options > Customize Ribbon and tick Developer in the right-hand list. Labels can differ slightly by version and region, so look for the equivalent wording if yours reads differently.
- Access to the Visual Basic Editor, opened with Alt+F11 on Windows. On a Mac, use Tools > Macro > Visual Basic Editor from the Excel menu bar; the shortcuts and dialog positions are different from Windows throughout.
- Macro security permission. If your organisation blocks macros entirely, the export macro will not run and your only option is the manual right-click method.
- Trust access to the VBA project object model, only if you plan to script the export from outside Excel (for example from PowerShell or a .NET utility). The built-in export macro described below does not need this setting.
- A folder to receive the files. Create it before you start so you can see immediately whether the export worked.
Step-by-Step
- Save and back up the workbook. Make a copy of the file before touching anything. Exporting does not modify your code, but you will be running a macro inside a project you care about, and a backup costs nothing.
- Create the destination folder. For example, make a folder called VBA Export next to the workbook. Note the full path, because the export routine needs it. Avoid folder names with spaces or accented characters if you can; they are legal but they make the quoting in the code easy to get wrong.
- Open the Visual Basic Editor. Press Alt+F11 (Windows) or use Tools > Macro > Visual Basic Editor (Mac). In the Project Explorer on the left, expand your workbook node. You should see folders for Microsoft Excel Objects, Forms, Modules and Class Modules. Everything under those folders is what will be exported.
- Insert a temporary standard module. Right-click anywhere in the Project Explorer, choose Insert > Module, and name it something obvious such as
modExportAll. This module is the tool you are about to run; it will also export itself, which is harmless. - Paste the export routine. In the code window, paste the following, changing the folder path to your own:
Sub ExportAllVBA()
Dim comp As VBComponent
Dim dest As String
Dim count As Long
dest = "C:\VBA Export\" ' must end with a backslash
If Right(dest, 1) <> "\" Then dest = dest & "\"
If Dir(dest, vbDirectory) = "" Then
MsgBox "Folder not found: " & dest
Exit Sub
End If
For Each comp In ThisWorkbook.VBProject.VBComponents
If comp.Type = 100 Then ' 100 = document module
comp.Export dest & comp.Name & ".cls"
Else
comp.Export dest & comp.Name & ExtensionFor(comp)
End If
count = count + 1
Next comp
MsgBox count & " components exported to " & dest
End Sub
Private Function ExtensionFor(comp As VBComponent) As String
Select Case comp.Type
Case 1: ExtensionFor = ".bas" ' standard module
Case 2: ExtensionFor = ".cls" ' class module
Case 3: ExtensionFor = ".frm" ' user form
Case Else: ExtensionFor = ".cls"
End Select
End Function
- Run it. Click inside the subroutine and press F5, or use Run > Run Sub/UserForm. A message box should report how many components were exported. If it reports zero, you are running it in the wrong project or the workbook genuinely has no code.
- Check the folder. You should see one file per module, class and form, named after the component. User forms also produce a matching
.frxfile that holds the form's binary layout; the.frmfile alone is not enough to restore a form. - Delete the temporary module. Right-click
modExportAllin the Project Explorer, choose Remove modExportAll, and decline to export it when prompted. Then save the workbook.
Common Mistakes
The most frequent problem is running the macro from the wrong project. The line ThisWorkbook.VBProject refers to the workbook containing the code, so if you paste the routine into Personal.xlsb, it exports Personal.xlsb, not your file. If you need to target a different open workbook, replace ThisWorkbook with Workbooks("YourFile.xlsm").
Second, the trailing backslash. Joining a folder path without it produces something like C:\VBA ExportModule1.bas, and the export fails with a permission or path error that looks unrelated to the real cause.
Third, forgotten user form binaries. Copying only .frm files leaves you with forms that open empty or refuse to load. Always keep the .frx alongside.
Fourth, expecting an .xlsx to work. If the workbook was saved without macros, there is no VBA project to export and the routine will tell you so. Finally, do not confuse this with exporting a whole project via File > Export File in the editor, which saves one component at a time only.
Tips
Give the destination folder a timestamp in its name (for example VBA Export 2026-12-04) so successive exports do not silently overwrite earlier snapshots. Plain files are also easy to put under version control: initialise a Git repository in the export folder and commit after each significant change. Because exported modules are text, you can diff two versions and see exactly which lines changed, which is impossible with a binary workbook.
If you only need code and not the workbook, export to a fresh folder, then import the modules into a clean .xlsm later using File > Import File in the editor. Keep a note of which references the project uses under Tools > References, since those settings are not stored in the exported files and must be re-ticked after a rebuild.
Encrypted or password-protected VBA projects cannot be exported until the password is supplied. If the project is locked and you do not know the password, there is no legitimate workaround; ask whoever owns the file.
Troubleshooting
"Programmatic access to Visual Basic Project is not trusted." This appears when code tries to reach the VBProject object model. In Excel go to File > Options > Trust Center > Trust Center Settings > Macro Settings and tick Trust access to the VBA project object model. Be aware this lowers a security barrier, so switch it back off when you are done and only enable it for files you trust.
The macro runs but writes nothing. Check that the folder path exists exactly as typed, including the drive letter, and that you are not writing to a protected location. Try a simple path like C:\Temp\ to isolate the issue.
Permission denied on export. A file with the same name is often open in another program or marked read-only. Close it or delete it and rerun.
Nothing happens at all when you press F5. The cursor may not be inside the subroutine, or macros are disabled. Check the macro security banner and enable content for this session only.
If that did not work, fall back to the manual method: in the Project Explorer, right-click each component and choose Export File, accepting the suggested extension. It is slow but needs no trust settings and no code.
Frequently Asked Questions
Can I export VBA code without running a macro?
Yes, one component at a time, using right-click > Export File in the Visual Basic Editor. There is no built-in command that exports an entire project in a single action.
What file extensions does the export produce?
Standard modules become .bas, class modules and document modules become .cls, and user forms become .frm plus a .frx companion file.
Does exporting remove code from my workbook?
No. Export writes a copy to disk and leaves the project untouched. Removing the temporary export module afterwards is the only change.
Can I export from an .xlsx file?
Not meaningfully, because an .xlsx cannot contain VBA. Save it as .xlsm first if it holds code, or there will be nothing to export.
Can I re-import the files later?
Yes. In the editor use File > Import File and select the exported .bas, .cls or .frm. Import forms together with their .frx so the layout survives.
Do the exported files preserve my references and passwords?
No. Library references, project passwords and some project properties live outside the module files and must be set again after rebuilding a project.