To save an Excel workbook as a VBA code file, you must export the VBA project modules to individual .bas, .cls, or .frm files using the Visual Basic for Applications editor, as Excel does not allow saving the entire workbook as a single VBA code file directly.

What You'll Need

  • Microsoft Excel (2010 or later recommended)
  • A workbook containing VBA macros or userforms
  • Access to the Developer tab in Excel
  • A destination folder to save the exported VBA files

Step-by-Step

  1. Open the Excel workbook that contains the VBA code you want to save.
  2. Press ALT + F11 to open the Visual Basic for Applications (VBA) editor.
  3. In the VBA editor, locate the Project Explorer pane (usually on the left). If it's not visible, press CTRL + R to show it.
  4. Right-click on the VBAProject (your workbook's name) in the Project Explorer.
  5. From the context menu, select Export File...
  6. In the export dialog box, choose a folder where you want to save the VBA files.
  7. Select each module, class module, or userform you want to export one at a time:
    • For standard modules: right-click the module (e.g., Module1) and choose Export File...
    • For class modules: right-click the class (e.g., clsMyClass) and choose Export File...
    • For userforms: right-click the form (e.g., UserForm1) and choose Export File...
  8. Each item will be saved as a separate file with the appropriate extension:
    • Standard modules: .bas
    • Class modules: .cls
    • Userforms: .frm
    • Note: .frx files (binary form data) may also be generated if the form contains controls; these are needed for the form to work properly.
  9. Repeat the export process for every VBA component you wish to save.
  10. Optionally, export any referenced references by noting them manually (Tools > References in the VBA editor), as these are not exported with the code files.
  11. Click Save for each file.
  12. After exporting all necessary components, close the VBA editor.
  13. To preserve the workbook itself, save it normally as an .xlsm file (if it contains macros) via File > Save As.

Common Mistakes

  • Attempting to save the .xlsm file as a .bas or .txt file directly — this corrupts the workbook and does not extract usable VBA code.
  • Forgetting to export userforms and class modules, resulting in incomplete code when reimporting.
  • Overlooking the .frx files associated with userforms, which store control properties and are required for the form to load correctly.
  • Assuming that exporting a module saves all related code — each module must be exported individually.
  • Not documenting external references (like Microsoft Scripting Runtime), which must be re-enabled manually after importing.

Tips

  • Use a consistent naming convention for exported files to make reimport easier.
  • Create a dedicated folder for each workbook’s VBA export to avoid mixing files from different projects.
  • After exporting, open a .bas file in Notepad++ or VS Code to verify the code is readable and complete.
  • Consider using source control (like Git) for your .bas, .cls, and .frm files to track changes over time.
  • To reimport later, use Import File... in the VBA editor’s Project Explorer context menu.
  • If you only need to share the code, exporting modules is safer than sharing the .xlsm file, as it avoids macro security prompts.

Troubleshooting

  • Cannot find the Developer tab: Go to File > Options > Customize Ribbon and check the box for Developer under Main Tabs.
  • VBA editor is blank or missing modules: Ensure the workbook is saved as a macro-enabled file (.xlsm). .xlsx files cannot contain VBA code.
  • Exported .bas file is empty: You may have selected a folder or empty module — double-check that the module contains code before exporting.
  • Userform fails to load after reimport: Confirm that both the .frm and corresponding .frx file are in the same folder and imported together.
  • Compile errors after reimport: Check that all referenced libraries (Tools > References) are available and checked on the target machine.
  • Export option is grayed out: This can happen if the workbook is protected or if you're in design mode incorrectly — ensure the workbook is unprotected and you're not in cell editing mode.

Frequently Asked Questions

Can I save the entire VBA project as a single file?

No, Excel does not support exporting the full VBA project to one file. Each module, class, and form must be exported separately.

Do I need to export .frx files?

Yes, if your userforms contain controls (like buttons or text boxes), the .frx file stores the control layout and must be exported alongside the .frm file.

Can I automate the export of all VBA modules?

Yes, using a VBA script that loops through the VBComponents collection and exports each one programmatically — but this requires trust access to the VBA project object model.

Is it safe to share exported .bas files?

Yes, .bas, .cls, and .frm files are plain text and pose no security risk by themselves — unlike .xlsm files, they cannot execute macros until imported into Excel.

Will exporting VBA code remove it from my workbook?

No, exporting creates a copy; the original code remains in the workbook unless you manually delete it.

What if I get a 'permission denied' error when exporting?

Ensure the destination folder is not read-only and that you have write permissions. Try saving to your Documents or Desktop folder.