Excel VBA Save As XLSX File Format Guide
In this article
To save a workbook as an .xlsx file using VBA, you must use the SaveAs method and specify the FileFormat parameter as xlOpenXMLWorkbook (which corresponds to the value 51). This is essential because .xlsx files are macro-free; if you attempt to save a workbook containing VBA code as an .xlsx without explicitly handling the file format and alerts, Excel will trigger a warning stating that the VBA project cannot be saved in that format.
What You'll Need
- Microsoft Excel installed on a Windows or macOS computer.
- A workbook containing data that you wish to export or save.
- Access to the Visual Basic for Applications (VBA) editor (Alt + F11).
- Basic familiarity with where you want to save your files on your local drive.
Step-by-Step
- Open the VBA Editor: Open your Excel workbook. Press
Alt + F11on your keyboard to launch the Visual Basic Editor. If you are on a Mac, you may need to use the Developer tab on the ribbon to open the editor. - Insert a New Module: In the Project Explorer window on the left, right-click on your workbook name, select
Insert, and then clickModule. This creates a clean space to write your macro without interfering with sheet-specific code. - Disable Excel Alerts: Before the save command, you must add
Application.DisplayAlerts = False. This is the most critical step. Because an .xlsx file cannot store macros, Excel will normally pop up a warning asking if you are sure you want to save in a macro-free format. Setting alerts to false suppresses this popup and allows the macro to proceed automatically. - Define the File Path and Name: Create a string variable to hold the full path where the file will be saved, including the filename and the
.xlsxextension. For example:Dim myPath As String: myPath = "C:\Users\YourName\Documents\ExportedData.xlsx". Ensure the folder path exists, or the code will return a runtime error. - Execute the SaveAs Method: Use the
ActiveWorkbook.SaveAscommand. You must specify theFileFormatargument. The code should look like this:ActiveWorkbook.SaveAs Filename:=myPath, FileFormat:=xlOpenXMLWorkbook. UsingxlOpenXMLWorkbooktells Excel specifically to use the .xlsx standard. - Re-enable Excel Alerts: Immediately after the save command, add
Application.DisplayAlerts = True. Failing to do this will leave Excel in a state where it will not warn you about other critical issues, such as overwriting existing files or losing unsaved changes in other workbooks. - Test the Macro: Place your cursor inside the subroutine and press
F5to run it. Check your destination folder to verify that the file has been created with the correct extension and that the data is intact.
Common Mistakes
One of the most frequent errors is forgetting to change the FileFormat parameter. If you only change the file extension in the filename string (e.g., naming it "test.xlsx") but leave the FileFormat as the default (which is often xlOpenXMLWorkbookMacroEnabled for .xlsm files), Excel will create a file that has an .xlsx extension but is internally structured as an .xlsm file. This leads to "File extension does not match file format" errors when the user tries to open the document.
Another common mistake is hard-coding the file path. If you share the workbook with a colleague, a path like C:\Users\John\Documents will fail on their computer because the username is different. To avoid this, use environment variables or the ThisWorkbook.Path property to create a dynamic path that works regardless of who is running the macro.
Users often forget to handle the DisplayAlerts property. If you leave alerts enabled, the macro will pause at the "Privacy Warning" or the "Macro-free workbook" warning. If the macro is running as part of a larger automated process, this pause can cause the entire system to hang or time out, as it waits for a human to click "Yes" on a dialog box that might be hidden behind other windows.
Tips
To make your macro more flexible, use the Application.GetSaveAsFilename method. Instead of hard-coding a path, this opens the standard Windows "Save As" dialog box, allowing the user to choose the folder and name the file. The method returns a string of the chosen path, which you can then pass into the SaveAs command.
If you are saving a copy of the data while wanting to keep the original macro-enabled workbook open, consider using Workbook.SaveCopyAs. However, be aware that SaveCopyAs does not allow you to change the file format; it creates an exact binary duplicate. To truly convert to .xlsx, you must use SaveAs, which changes the active workbook to the new file. To return to the original, you may need to save the .xlsx version, close it, and then reopen your .xlsm master file.
For those managing large amounts of data, consider using ActiveSheet.Copy first. This moves the target sheet to a brand new, temporary workbook. You can then save that new workbook as an .xlsx and close it. This prevents your master macro-enabled workbook from being converted into a macro-free file in the current session, meaning you don't have to reopen your original file to continue working.
Troubleshooting
If you encounter a "Runtime Error 1004: Method 'SaveAs' of object '_Workbook' failed," first check if the destination folder actually exists. VBA cannot create new folders using the SaveAs command; it can only save files into existing directories. If the path is wrong or the folder was deleted, the code will crash.
Check for file locks. If the file you are trying to overwrite is already open in another instance of Excel or by another user on a network drive, VBA will be unable to save over it. Ensure the target file is closed before running the macro. You can add a basic error handler using On Error Resume Next to detect if the file is locked and notify the user with a MsgBox.
If the resulting file is corrupted or won't open, verify that you used xlOpenXMLWorkbook (value 51). If you accidentally used xlWorkbookDefault, Excel might use a format incompatible with the .xlsx extension you provided in the filename string. Always explicitly define the format when changing extensions.
If the macro runs but no file appears, check if you have OneDrive or SharePoint synchronization active. Sometimes files are saved to a cloud-synced folder and may take a few seconds to appear in the local File Explorer, or they may be saved to a URL-based path (https://...) which requires different handling than a standard local drive path.
Frequently Asked Questions
Can I save a workbook as .xlsx and keep the macros in the same file?
No. The .xlsx format is strictly macro-free by design for security reasons. To keep macros, you must save the file as .xlsm (Excel Macro-Enabled Workbook) or .xlsb (Excel Binary Workbook).
What is the difference between xlOpenXMLWorkbook and xlWorkbookDefault?
xlOpenXMLWorkbook specifically forces the .xlsx format. xlWorkbookDefault uses whatever the current default save format is in the user's Excel options, which can vary between computers and versions.
How do I save only one specific sheet as an .xlsx file?
The best method is to use Sheets("SheetName").Copy. This command creates a new workbook containing only that sheet. You can then call ActiveWorkbook.SaveAs on that new workbook and close it immediately after.
Why does my code still show a warning about macros being removed?
This happens if Application.DisplayAlerts = False is not placed before the SaveAs line. Ensure the alert suppression is active at the exact moment the save command is executed.
Will saving as .xlsx remove my formulas?
No, .xlsx files preserve all formulas, formatting, and data. Only the VBA modules, UserForms, and macro code are stripped away during the saving process.
Can I use this method to save a file to a shared network drive?
Yes, as long as the computer running the macro has write permissions for that network folder and the path is provided as a UNC path (e.g., \\Server\Folder\File.xlsx) or a mapped drive letter.