Smart Systems for Home

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

Excel VBA: Save as Macro-Enabled Workbook

· 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 file as a macro-enabled workbook with Excel VBA, call the SaveAs method on the ThisWorkbook or ActiveWorkbook object and pass the FileFormat:=xlOpenXMLWorkbookMacroEnabled argument, then set CreateBackup:=False and supply the full path with a .xlsm extension. That single line preserves your VBA project; saving with the default .xlsx format strips the macros and Excel warns you first. The rest of this guide covers the exact syntax, the pitfalls that silently break the macro project, and how to recover when the save fails.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook that already contains VBA code, or a blank workbook you will add code to.
  • Macro security set to allow macros, at least for the session (File > Options > Trust Center > Trust Center Settings > Macro Settings).
  • Write permission to the destination folder; saving to a read-only network share will fail with a runtime error.

Step-by-Step

  1. Open the workbook that contains your macros. If you are starting fresh, press Alt+F11 (Windows) or Option+F11 (Mac) to open the VBA editor, insert a module, and paste a test macro such as Sub Hello(): MsgBox "Saved": End Sub so there is something to preserve.
  2. Open the VBA editor and insert a module. In the Project Explorer, right-click your workbook name, choose Insert > Module. A standard module is where the save routine will live. Do not put the save code in the ThisWorkbook object if you plan to call it from a button; a standard module keeps it portable.
  3. Write the save procedure. Type the following, replacing the path with your own folder:
    Sub SaveAsMacroEnabled()
        Dim savePath As String
        savePath = "C:\Reports\MyWorkbook.xlsm"
        ThisWorkbook.SaveAs _
            Filename:=savePath, _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
            CreateBackup:=False
    End Sub
    The FileFormat constant is the critical part. xlOpenXMLWorkbookMacroEnabled is the named constant for the .xlsm format; the numeric value is 52, but use the constant for readability.
  4. Use ThisWorkbook versus ActiveWorkbook deliberately. ThisWorkbook always refers to the workbook containing the running code, which is almost always what you want. ActiveWorkbook refers to whatever window has focus and can save the wrong file if the user clicks another workbook mid-run.
  5. Add error handling. Wrap the save in On Error GoTo SaveFailed and a handler that shows Err.Description. A failed save on a network path or a locked file otherwise leaves the user staring at a generic error dialog with no clue which folder failed.
  6. Run the macro. Press F5 in the editor or assign the macro to a button on the sheet (Developer > Insert > Button Form Control). Confirm the file appears in the target folder with the .xlsm extension, not .xlsx.
  7. Verify the macros survived. Close the workbook, reopen it, and check that the Developer tab still lists your macro and that the yellow macro-enabled banner does not say macros were removed. If the extension shows .xlsx, the save used the wrong format and the VBA project is gone.
  8. Optionally save a copy instead. If you want to keep the original open and write a snapshot elsewhere, use ThisWorkbook.SaveCopyAs "C:\Reports\Backup.xlsm". SaveCopyAs writes a copy without changing the open file's path or name.

Common Mistakes

  • Omitting the FileFormat argument. Excel then defaults to the current format or, on a new workbook, .xlsx, and the macro project is discarded. Always pass the constant explicitly.
  • Passing a .xlsm path with the .xlsx format constant. The extension and the format must agree; mismatches produce a runtime error 1004 or a silently wrong file.
  • Using ActiveWorkbook in a loop or from an add-in. The active window can change between lines, so the macro saves the wrong file. Use ThisWorkbook.
  • Saving over an open file. If the destination .xlsm is already open in another Excel instance, the save fails with a permission error. Close the other copy first.
  • Forgetting CreateBackup:=False. With backups enabled, Excel writes a .xlk file alongside and can confuse users who expect only one output file.
  • Assuming the macro works on Mac unchanged. The path separator differs; use Application.PathSeparator or build paths with Environ("USERPROFILE") on Windows and MacScript-free alternatives on Mac. The FileFormat constant is the same on both platforms.

Tips

  • Build the path with ThisWorkbook.Path & Application.PathSeparator & "MyWorkbook.xlsm" so the macro works in any folder rather than a hard-coded C:\Reports.
  • Check Dir(savePath) before saving if you want to prompt the user before overwriting an existing file.
  • Use Application.DisplayAlerts = False only around the save line and set it back to True immediately; leaving it off hides every later confirmation dialog.
  • If you need to save as .xlsb (binary) instead, the constant is xlExcel12; the .xlsm constant is the one that keeps VBA in the standard XML format.
  • Test the macro on a copy of the workbook first; a wrong FileFormat can overwrite the only copy and strip its macros permanently.

Troubleshooting

If the macro runs but the file opens without macros, the save used .xlsx. Reopen the original, fix the FileFormat argument, and save again; the stripped copy cannot be repaired. If you get runtime error 1004 "Method 'SaveAs' of object '_Workbook' failed", the usual causes are a read-only destination, a missing folder, or an invalid character in the filename; create the folder first with MkDir or choose an existing one. If the error mentions the file is in use, close the other Excel window holding the target. If the macro is blocked entirely, check Trust Center macro settings and whether the file came from the internet; right-click the file, choose Properties, and unblock it if Windows marked it. If saving to OneDrive or a network share fails intermittently, save locally first and let OneDrive sync, since the cloud path can be locked during sync. If the Developer tab is missing, enable it from the ribbon customization options; the VBA editor itself is unchanged.

Frequently Asked Questions

What is the numeric value of xlOpenXMLWorkbookMacroEnabled?

It is 52. Use the named constant in code; the number is only useful when reading recorded macros.

Can I save as .xlsm without VBA code?

Yes. The format accepts a workbook with no macros, but the file will still carry the macro-enabled extension and Excel may warn about macro content depending on Trust Center settings.

Does SaveAs change the open workbook's name and path?

Yes. After SaveAs, the open workbook now points at the new file. Use SaveCopyAs if you want a copy without changing the current file.

Why does Excel warn that the file contains macros when I open it?

That is the standard macro security banner. It appears for any .xlsm file unless the folder is marked as trusted or macros are enabled in Trust Center.

Can I save as .xlsm from a macro on Excel for Mac?

Yes, the FileFormat constant works the same. Only the path syntax differs; use Application.PathSeparator to stay portable.

What happens if I use the .xlsx format constant by mistake?

Excel saves the workbook without its VBA project and shows a warning that macros cannot be saved in that format. The original macros are lost in the new file, though the old file remains if you saved under a new name.