To save a workbook as an .xlsx file using VBA, you must use the SaveAs method and specify the FileFormat as xlOpenXMLWorkbook (which corresponds to the value 51). This process is essential when you have a macro-enabled workbook (.xlsm) but need to export a clean version of the data without the VBA code for distribution to users who cannot or should not run macros.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- A workbook containing the data you wish to save.
- Access to the Visual Basic for Applications (VBA) editor.
- Basic familiarity with how to open the Developer tab in the Excel ribbon.
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard. This is the universal shortcut to jump from the Excel spreadsheet interface into the VBA backend. - Insert a New Module: In the Project Explorer pane on the left, right-click on your workbook name, hover over
Insert, and selectModule. Writing your code in a standard module ensures the macro is available globally across the workbook. - Write the SaveAs Procedure: Copy and paste the following code into the module window. Note that labels and menu paths may differ slightly depending on your version of Excel or regional settings.
Sub SaveAsXLSX() Dim filePath As String Dim fileName As String ' Define the folder path and file name filePath = "C:\Users\YourName\Documents\" fileName = "ExportedData.xlsx" ' Turn off alerts to prevent the "Macro-free workbook" warning Application.DisplayAlerts = False ' Save the workbook in the .xlsx format (FileFormat 51) ActiveWorkbook.SaveAs Filename:=filePath & fileName, _n FileFormat:=xlOpenXMLWorkbook ' Turn alerts back on Application.DisplayAlerts = True MsgBox "Workbook saved successfully as XLSX!" End Sub - Customize the File Path: Change the
filePathvariable in the code to a valid folder on your computer. Ensure the path ends with a backslash (\). If you want to save it in the same folder as the current workbook, usefilePath = ThisWorkbook.Path & "\". - Run the Macro: You can run the code by pressing
F5while the cursor is inside the subroutine, or by returning to Excel, going to theDevelopertab, clickingMacros, selectingSaveAsXLSX, and clickingRun. - Verify the Output: Navigate to the folder you specified and confirm that a new file with the .xlsx extension has been created. Open it to ensure the data is intact and that the file no longer contains the VBA macros.
Common Mistakes
One of the most frequent errors is forgetting to change the FileFormat parameter. If you simply change the file extension in the filename string to .xlsx but leave the FileFormat as the default (usually xlOpenXMLWorkbookMacroEnabled or 52), Excel will save a macro-enabled file with a misleading extension. When the user tries to open it, Excel will throw a corruption error stating that the file extension does not match the file format.
Another common mistake is failing to handle Application.DisplayAlerts. When you save a workbook containing macros as a standard .xlsx file, Excel triggers a warning popup informing you that all VBA projects will be removed. If your macro is intended to run automatically or as part of a larger batch process, this popup will halt the code execution until a user manually clicks "Yes." Setting DisplayAlerts = False suppresses this warning, allowing the code to run seamlessly.
Hard-coding file paths is also a frequent pitfall. If you share the workbook with a colleague and the code points to C:\Users\YourName\..., the macro will crash on their machine because that specific user directory does not exist. Always use dynamic paths like ThisWorkbook.Path or Environ("USERPROFILE") to ensure the code works across different computers.
Tips
To make your macro more professional, use the Application.GetSaveAsFilename method. Instead of hard-coding a name, this opens the standard Windows "Save As" dialog box, allowing the user to choose the destination folder and name the file themselves. The method returns a string containing the path the user chose, which you can then pass into the SaveAs command.
If you are saving a specific sheet rather than the whole workbook, do not use ActiveWorkbook.SaveAs. Instead, use ActiveSheet.Copy. This command creates a brand new workbook containing only that specific sheet. You can then call the SaveAs method on the newly created ActiveWorkbook and close it immediately after saving. This prevents your original master file from being renamed or replaced by the .xlsx version.
For those handling large volumes of data, consider adding Application.ScreenUpdating = False at the start of your macro and setting it back to True at the end. This stops the screen from flickering while Excel performs the save operation, which slightly improves performance and provides a cleaner user experience.
Troubleshooting
If the code fails with a "Run-time error 1004," the most likely cause is an invalid file path. Check that the folder you specified actually exists and that you have write permissions for that directory. Ensure there are no illegal characters in the filename, such as asterisks, question marks, or slashes.
If you receive a "Permission Denied" error, it usually means the file you are trying to overwrite is already open in another instance of Excel or by another user on a network drive. Close all open versions of the target .xlsx file and try running the macro again.
If the file saves but still contains macros, double-check your FileFormat argument. It must be xlOpenXMLWorkbook or the integer 51. If you use xlOpenXMLWorkbookMacroEnabled (52), the file will remain a macro-enabled workbook regardless of the file extension used in the filename string.
If the macro runs but the file is not created, check if your antivirus or OneDrive synchronization is blocking the VBA process from writing to the disk. Try saving to a local folder (like a temporary folder on the C: drive) to see if the issue is related to network or cloud permissions.
Frequently Asked Questions
Can I save as .xlsx and keep my macros?
No. The .xlsx format is specifically designed to be macro-free for security reasons. To keep your macros, you must save the file as .xlsm (Macro-Enabled Workbook) or .xlsb (Binary Workbook).
What is the difference between FileFormat 51 and 52?
FileFormat 51 refers to xlOpenXMLWorkbook (.xlsx), which contains no macros. FileFormat 52 refers to xlOpenXMLWorkbookMacroEnabled (.xlsm), which allows the storage of VBA code.
Will saving as .xlsx delete the macros from my original file?
If you use ActiveWorkbook.SaveAs, the current session switches to the new .xlsx file, and the macros are removed from that new file. However, the original .xlsm file on your hard drive remains unchanged unless you explicitly overwrite it.
How do I save only one sheet as an .xlsx file?
Use Sheets("SheetName").Copy to move the sheet to a new workbook, then use the SaveAs method on that new workbook, and finally use ActiveWorkbook.Close SaveChanges:=False.
Can I automate this to happen every time I close the workbook?
Yes, you can place the save code inside the Workbook_BeforeClose event in the ThisWorkbook module, though this is generally discouraged as it may create duplicate files every time the user exits.