Smart Systems for Home

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

Excel VBA Save As Macro Enabled Workbook: How-To

· 5 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 an Excel workbook as a macro-enabled file, use the Save As dialog and choose the Excel Macro-Enabled Workbook (*.xlsm) format, or let VBA do it for you with ThisWorkbook.SaveAs Filename:="C:\Path\File.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled. The key is the .xlsm extension and the matching FileFormat constant; saving as .xlsx strips out every macro in the file. This guide covers the manual route, the VBA route, and the traps that silently delete your code.

What You'll Need

  • Excel on Windows or Mac (the VBA editor is included in both; the menu labels differ slightly by version and region).
  • A workbook that already contains macros, or one you are about to add them to.
  • Macro security set to allow macros to run — otherwise your code will be saved but blocked from executing. Check under File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows, or Excel > Preferences > Security on Mac.
  • Write access to the folder you are saving into. Network shares and synced cloud folders sometimes refuse the save.

Step-by-Step

Method 1: Save manually with the dialog

  1. Open the workbook that contains your VBA code.
  2. Press F12 (Save As). On Mac, use Command+Shift+S or File > Save As.
  3. In the Save as type dropdown, select Excel Macro-Enabled Workbook (*.xlsm). Do not pick the plain .xlsx option.
  4. Choose a folder, type a file name, and click Save.
  5. If Excel warns that the file contains macros, confirm with Yes to keep the macro-enabled format.

Method 2: Save with a VBA macro

  1. Press Alt+F11 to open the VBA editor (on Mac, Option+F11 or Developer > Visual Basic).
  2. Insert a new module: Insert > Module.
  3. Paste a procedure such as:
    Sub SaveAsMacroEnabled()
        Dim path As String
        path = "C:\Reports\MyWorkbook.xlsm"
        ThisWorkbook.SaveAs Filename:=path, _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End Sub
  4. Replace the path with a folder that exists and that you can write to. The folder must already exist; SaveAs will not create it.
  5. Run the macro with F5 while the cursor is inside the procedure, or assign it to a button.
  6. Confirm the file now ends in .xlsm and that reopening it still shows your modules in the VBA editor.

Method 3: Save a copy without changing the open file

  1. Use ThisWorkbook.SaveCopyAs "C:\Reports\Backup.xlsm" when you want a snapshot and want to keep working in the current file.
  2. Note that SaveCopyAs does not change the open workbook's name or path, and it does not prompt for a format — so include the .xlsm extension yourself.

Common Mistakes

  • Choosing .xlsx by habit. Excel will save the file but discard all VBA. You usually get a warning; if you click through it, the code is gone from that copy.
  • Forgetting the FileFormat argument. SaveAs without FileFormat can default to the current format or prompt the user. Always pass xlOpenXMLWorkbookMacroEnabled.
  • Wrong constant name. The constant is xlOpenXMLWorkbookMacroEnabled (value 52). Typing xlWorkbookMacroEnabled or a similar guess raises a compile error.
  • Missing folder. SaveAs fails with a runtime error if the target directory does not exist. Create it first with MkDir or check with Dir.
  • Saving to a read-only location. Files opened from email attachments or protected network shares open read-only; SaveAs to a new local path instead.
  • Assuming .xlsm runs macros automatically. Macro security still applies. A file saved correctly can still be blocked by Trust Center settings or Mark of the Web on downloaded files.

Tips

  • Build the path with ThisWorkbook.Path & "\" & "Name.xlsm" so the macro saves alongside the current file rather than to a hard-coded drive.
  • Use Application.DisplayAlerts = False around automated saves to suppress the format prompt, then set it back to True immediately afterwards. Leaving alerts off hides real errors.
  • Test the macro on a copy first. A SaveAs pointed at the wrong path can overwrite an existing file without asking when alerts are disabled.
  • Keep a version with the code in a trusted folder, and sign your VBA project with a digital certificate if you distribute the workbook.
  • If you need to share data without code, save a separate .xlsx copy via SaveCopyAs or Save As — but remember that copy will not contain macros.

Troubleshooting

The macro errors with "Method 'SaveAs' of object '_Workbook' failed". Check the folder exists, the path has no invalid characters, the file is not open elsewhere, and you have write permission. Try a simple local path such as C:\Temp\test.xlsm to isolate the cause.

The file saves but macros are missing on reopen. Confirm the extension really is .xlsm and not .xlsx. In File Explorer, turn on file name extensions to see the true type. If it is .xlsx, re-save with the macro-enabled format.

Excel says macros are disabled. Open File > Options > Trust Center > Trust Center Settings > Macro Settings and enable macros for the session, or add the folder to Trusted Locations. On Mac, check Excel > Preferences > Security. Do not lower security globally for files you do not trust.

SaveAs prompts for a format every time. You likely omitted FileFormat, or the path extension does not match the constant. Make both agree: .xlsm with xlOpenXMLWorkbookMacroEnabled.

The workbook opens read-only. It may be marked as final, downloaded from the internet, or stored on a share without write access. Use Save As to a local folder you control.

Frequently Asked Questions

What is the FileFormat value for a macro-enabled workbook?

xlOpenXMLWorkbookMacroEnabled, which equals 52. Use it with the .xlsm extension.

Can I save as .xlsm without VBA?

Yes. Use File > Save As and pick Excel Macro-Enabled Workbook (*.xlsm) from the type list. The file simply contains no macros until you add them.

Does saving as .xlsm keep my macros?

Yes, that is the point of the format. Saving as .xlsx or .csv removes VBA code from the saved copy.

Why does SaveAs fail when the folder is missing?

SaveAs does not create directories. Create the folder first, for example with MkDir, then call SaveAs.

Can I save to OneDrive or a network share with VBA?

Usually yes, if the path is available and writable. Synced cloud folders can occasionally lock files; if SaveAs fails, save locally and let the sync client upload it.

What happens if I use SaveCopyAs instead of SaveAs?

SaveCopyAs writes a copy to disk without changing the open workbook. It does not prompt for format, so include .xlsm in the path to keep macros in the copy.