VBA & Macro Problems

How to Save an XLSM Workbook as XLSX with the Same Filename via VBA

Natalie TaylorNatalie Taylor Nov 3, 2026 869 views

Question details

The user wants to create an Excel VBA macro that copies the active worksheet into a new workbook and saves it as an XLSX file in the same folder with the identical base filename as the original XLSM file.

How to Save an XLSM Workbook as XLSX with the Same Filename via VBA
Product
Excel
Device & OS
not provided
Scenario
Automating the conversion of an XLSM workbook to a macro-free XLSX format without manually retyping the filename or navigating directory structures, typically triggered by clicking a worksheet button.
Observed behavior
The user needs the VBA SaveAs method to dynamically inherit the original workbook's directory path and base filename instead of relying on a hardcoded, fixed file path.
Before you start

Ensure that macros are enabled in your Excel settings and that your original XLSM workbook has been saved at least once so it has a valid directory path.

Solution 1Recommended

Automate Saving XLSM as XLSX using a VBA Macro

Use a custom VBA script to dynamically extract the original path and filename, copy the active sheet, and save it as an XLSX file.

When automating file conversions in Excel, VBA allows you to dynamically retrieve the current workbook's path using 'ThisWorkbook.Path' and its name using 'ThisWorkbook.Name'. This ensures your code works universally, even if the file is moved to another folder or shared with colleagues.

1
Open the VBA Editor

Press 'Alt + F11' on your keyboard to open the Microsoft Visual Basic for Applications window.

2
Insert a New Module

Click 'Insert' in the top menu and select 'Module' to create a blank canvas for your code.

3
Write the Macro Code

Create a Sub procedure. Declare strings for your path and filename. Use 'Replace(ThisWorkbook.Name, ".xlsm", "")' to extract the base name. Then, use 'ActiveSheet.Copy' to move the sheet to a new workbook.

4
Apply the SaveAs Method

In the new workbook object, use the '.SaveAs' method. Specify your concatenated path and filename, and critically, set 'FileFormat:=xlOpenXMLWorkbook' (which correlates to the XLSX format).

5
Assign to a Button

Go back to your worksheet, navigate to the 'Developer' tab, click 'Insert', and choose 'Button (Form Control)'. Draw it on the sheet and assign your new macro to it.

Automate Saving XLSM as XLSX using a VBA Macro
Prevent Save Prompts: To prevent Excel from showing a warning when saving a macro-enabled workbook as a macro-free workbook, wrap your SaveAs method with 'Application.DisplayAlerts = False' before saving, and set it back to 'True' afterward.
Advanced Spreadsheet Capabilities

Automate Your Workbooks with WPS Spreadsheet

WPS Spreadsheet provides robust support for macros and VBA, allowing you to automate repetitive tasks like file conversions easily. Experience high compatibility with Microsoft Excel files in a lightweight application.

  1. 1. Open Your XLSM File: Launch WPS Spreadsheet and open your macro-enabled workbook.
  2. 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon and click on 'Visual Basic Editor'.
  3. 3. Insert the Macro Code: Paste your VBA code to copy the active sheet and save it as an XLSX file.
  4. 4. Run the Automation: Run the macro directly or assign it to a form control button for one-click file conversion.
Fully compatible with Microsoft Excel formats including .xlsx, .xlsm, and .xls.Supports standard VBA macros for advanced workflow automation.Lightweight application that runs smoothly on older devices.Free to download with a highly intuitive, tabbed interface.
microsoft office alternative - wps office

Frequently Asked Questions

What is the VBA FileFormat code for an XLSX file?

The FileFormat property value for a standard XLSX file (Excel Workbook) is 'xlOpenXMLWorkbook', which corresponds to the numeric value 51.

Will my macros be saved in the new XLSX file?

No. By design, the XLSX format cannot contain macros. When you save an XLSM file as XLSX, all VBA code and macros are automatically stripped from the new file.

How do I assign my save macro to a button on the worksheet?

Go to the Developer tab, click Insert, and select 'Button (Form Control)'. Draw the button on your worksheet. When the 'Assign Macro' dialog box appears, select your newly created save macro and click OK.

Why am I getting an error when using SaveAs in VBA?

This often happens if the target filename already exists and Excel prompts for overwriting, or if you specify the '.xlsx' extension in the filename but use the wrong 'FileFormat' code. Ensure you use 'FileFormat:=xlOpenXMLWorkbook' when saving as XLSX.