Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Save a VBA Module in Excel

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To save a VBA module in Excel, you need to save the workbook as a macro-enabled file type (`.xlsm`) or, if you want the macros available in every workbook, export the module and import it into your Personal Macro Workbook. The exact steps depend on whether you want to keep the code inside the current file or reuse it elsewhere. Below is the full process for both methods, plus what to do when Excel refuses to save your macros.

What You'll Need

  • Excel for Windows or Mac (the VBA editor is not available in Excel for the web).
  • A workbook that already contains a VBA module, or permission to create one.
  • If macros are disabled by policy, you may need your IT administrator to allow them.

Step-by-Step

Method 1: Save the module inside the current workbook

  1. Open the workbook that contains the VBA code.
  2. Press Alt + F11 (Windows) or Fn + Option + F11 (Mac) to open the Visual Basic Editor. Menu labels can differ slightly by Excel version and region.
  3. In the Project Explorer on the left, find your workbook and expand the Modules folder. If you don't see the Project Explorer, press Ctrl + R (Windows) or use View > Project Explorer.
  4. Confirm the module contains the code you want to keep. You can rename it in the Properties window (press F4 to show it) by changing the (Name) field.
  5. Close the Visual Basic Editor and return to Excel.
  6. Go to File > Save As.
  7. In the Save as type dropdown, choose Excel Macro-Enabled Workbook (*.xlsm). If you save as a standard `.xlsx`, Excel will warn that macros cannot be saved and will strip the module.
  8. Choose a location, give the file a name, and click Save.
  9. If a dialog appears asking about macros, choose Yes to keep the VBA project.

Your module is now stored inside the workbook. Reopen the file later and the code will still be there, provided macros are enabled.

Method 2: Export the module as a separate file

  1. Open the Visual Basic Editor (Alt + F11 on Windows).
  2. In the Project Explorer, right-click the module you want to save.
  3. Choose Export File.
  4. In the dialog, pick a folder and confirm the file name. The file will have a `.bas` extension.
  5. Click Save. The `.bas` file now contains your module's code and can be imported into any other workbook.

To reuse it later, open the target workbook, go to the Visual Basic Editor, right-click the project, and choose Import File, then select the `.bas` file.

Method 3: Save the module to your Personal Macro Workbook

  1. Open Excel and record a dummy macro if you have never used the Personal Macro Workbook. Go to View > Macros > Record Macro, choose Personal Macro Workbook in the “Store macro in” dropdown, then stop recording.
  2. Open the Visual Basic Editor. You will now see PERSONAL.XLSB in the Project Explorer.
  3. Right-click VBAProject (PERSONAL.XLSB) and choose Insert > Module.
  4. Paste or write your code into the new module.
  5. Press Ctrl + S to save. Excel will prompt you to save the Personal Macro Workbook—click Yes.

Macros in the Personal Macro Workbook are available in every workbook you open on that computer. They are not shared with other users unless you copy the file.

Common Mistakes

  • Saving as `.xlsx`. This is the most common error. Excel removes the VBA project without warning beyond a brief message. Always use `.xlsm` for files with macros.
  • Forgetting to enable macros. If macros are disabled, the module may appear empty or missing when you reopen the file. Check the yellow security bar and click Enable Content.
  • Exporting the wrong module. Right-click the specific module, not the workbook node, before choosing Export File.
  • Assuming the Personal Macro Workbook syncs. It is stored locally on your computer and does not travel with your Excel file.
  • Ignoring Trust Center settings. If macros are blocked by policy, saving will not help. Check File > Options > Trust Center > Trust Center Settings > Macro Settings.

Tips

  • Name your modules descriptively (e.g., `modReports`) so you can find them quickly in the Project Explorer.
  • Use File > Info > Protect Workbook to add a password if the code is sensitive.
  • Keep a backup of exported `.bas` files in a version-controlled folder; it makes restoring or sharing code much easier.
  • If you work across multiple computers, store your `.xlsm` file in a cloud folder, but remember the Personal Macro Workbook does not sync automatically.
  • Test your saved file by closing and reopening it, then running the macro to confirm it survived the save.

Troubleshooting

Excel says “The following features cannot be saved in macro-free workbooks.” You are saving as `.xlsx`. Change the file type to `.xlsm` and save again.

The module is missing after reopening. Macros may be disabled. Look for the security warning bar and enable content. If the file was saved as `.xlsx`, the module is gone; you will need to restore from a backup or re-import the `.bas` file.

“Programmatic access to Visual Basic Project is not trusted.” This appears when code tries to modify the VBA project. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and check Trust access to the VBA project object model. Only do this if you understand the security implications.

Cannot save the Personal Macro Workbook. Close all other workbooks, then try saving again. If prompted that the file is in use, restart Excel.

Export File is greyed out. Make sure you right-clicked a module, not a worksheet or the workbook node. If the project is protected, you may need the password to export.

Frequently Asked Questions

Can I save a VBA module in an `.xlsx` file?

No. The `.xlsx` format does not support macros. You must save as `.xlsm` (macro-enabled workbook) or `.xlsb` (binary workbook).

Where is the Personal Macro Workbook stored?

It is typically in `%APPDATA%\Microsoft\Excel\XLSTART` on Windows. On Mac, it is in the user's Library/Application Support/Microsoft/Office/Excel/XLSTART folder. The exact path varies by Office version.

How do I share a module with someone else?

Export the module as a `.bas` file and send that file. The recipient can import it via the Visual Basic Editor. Alternatively, share the entire `.xlsm` workbook.

Does saving as `.xlsm` affect my formulas or data?

No. The `.xlsm` format is identical to `.xlsx` except that it also stores VBA code. All formulas, formatting, and data remain intact.

Why does Excel ask to save the Personal Macro Workbook every time I close it?

Because it contains unsaved changes. If you do not want to keep changes, choose “Don't Save” when prompted. To avoid the prompt, save the Personal Macro Workbook manually after editing.

Can I save a module from Excel for the web?

No. Excel for the web does not include the Visual Basic Editor. You need the desktop version of Excel to create, edit, or save VBA modules.