Excel VBA: Save as Macro-Enabled Workbook
In this article
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
- Open the workbook that contains your macros. If you are starting fresh, press
Alt+F11(Windows) orOption+F11(Mac) to open the VBA editor, insert a module, and paste a test macro such asSub Hello(): MsgBox "Saved": End Subso there is something to preserve. - 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
ThisWorkbookobject if you plan to call it from a button; a standard module keeps it portable. - Write the save procedure. Type the following, replacing the path with your own folder:
TheSub SaveAsMacroEnabled() Dim savePath As String savePath = "C:\Reports\MyWorkbook.xlsm" ThisWorkbook.SaveAs _ Filename:=savePath, _ FileFormat:=xlOpenXMLWorkbookMacroEnabled, _ CreateBackup:=False End SubFileFormatconstant is the critical part.xlOpenXMLWorkbookMacroEnabledis the named constant for the.xlsmformat; the numeric value is 52, but use the constant for readability. - Use
ThisWorkbookversusActiveWorkbookdeliberately.ThisWorkbookalways refers to the workbook containing the running code, which is almost always what you want.ActiveWorkbookrefers to whatever window has focus and can save the wrong file if the user clicks another workbook mid-run. - Add error handling. Wrap the save in
On Error GoTo SaveFailedand a handler that showsErr.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. - Run the macro. Press
F5in 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.xlsmextension, not.xlsx. - 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. - 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".SaveCopyAswrites a copy without changing the open file's path or name.
Common Mistakes
- Omitting the
FileFormatargument. 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
.xlsmpath with the.xlsxformat constant. The extension and the format must agree; mismatches produce a runtime error 1004 or a silently wrong file. - Using
ActiveWorkbookin a loop or from an add-in. The active window can change between lines, so the macro saves the wrong file. UseThisWorkbook. - Saving over an open file. If the destination
.xlsmis 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.xlkfile alongside and can confuse users who expect only one output file. - Assuming the macro works on Mac unchanged. The path separator differs; use
Application.PathSeparatoror build paths withEnviron("USERPROFILE")on Windows andMacScript-free alternatives on Mac. TheFileFormatconstant 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-codedC:\Reports. - Check
Dir(savePath)before saving if you want to prompt the user before overwriting an existing file. - Use
Application.DisplayAlerts = Falseonly around the save line and set it back toTrueimmediately; leaving it off hides every later confirmation dialog. - If you need to save as
.xlsb(binary) instead, the constant isxlExcel12; the.xlsmconstant is the one that keeps VBA in the standard XML format. - Test the macro on a copy of the workbook first; a wrong
FileFormatcan 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.