How to Keep the XLSX Extension When Renaming Files with VBA
Question details
The user needs to ensure that the .xlsx extension is retained when using a VBA script to rename Excel files.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Renaming workbook files in bulk or individually using a VBA procedure where the file extensions are unexpectedly dropped.
- Observed behavior
- The VBA file-renaming procedure removes the Excel workbook extension because the provided old and new names do not explicitly include the .xlsx suffix.
Verify that your Windows file explorer is set to show file extensions so you can easily confirm the format, and make a backup of the target files before running the macro.
Include the Complete Filename with the .xlsx Extension
Explicitly add the .xlsx extension to both the source and destination string values in your worksheet or VBA code so the file format remains intact.
When using the Name function in VBA to rename files, the operating system treats the new name as a literal string. If the extension is omitted from the new name variable, the resulting file will lose its file type association.
Identify the columns in your spreadsheet or the specific variables in your VBA script that hold the old and new file names.
Ensure the original file name contains its full extension, such as 'OldName.xlsx'.
Add the desired extension to the new file name, formatting it as 'NewName.xlsx'.
Run your VBA procedure (e.g., Name oldPath As newPath) to safely rename the workbook while keeping it an accessible Excel file.
Automate Tasks with WPS Spreadsheet Macros
WPS Office offers robust built-in support for VBA macros, allowing you to seamlessly automate file renaming and processing tasks just as you would in Microsoft Excel.
- 1. Open WPS Spreadsheet: Launch WPS Office and open a new or existing spreadsheet.
- 2. Access the Developer Tools: Navigate to the 'Tools' tab and click on 'Developer' to open the VBA Editor.
- 3. Insert your VBA code: Create a new Module and paste your file-renaming macro, ensuring your variables include the .xlsx extension.
- 4. Run the macro: Click 'Run' to execute the macro and safely rename your targeted files.

Frequently Asked Questions
Why did my file turn into a blank, unreadable file after running the VBA rename script?
If the new file name in your VBA script does not explicitly include the .xlsx extension, the operating system removes the file type association. Adding '.xlsx' to the new name string will resolve this and restore the Excel icon.
Can I use VBA to rename files to a different format, like .xls or .csv?
Changing the file extension using the VBA 'Name' statement only changes the file name string, not the underlying file encoding. To properly convert file formats, you must open the file in VBA and use the 'SaveAs' method with the appropriate FileFormat parameter.
How do I automatically append the extension in VBA instead of typing it manually in the sheet?
You can concatenate the extension directly in your VBA code using the ampersand operator. For example, you can write: newFileName = cell.Value & ".xlsx" before executing the renaming command.




