How to Use VBA to Rename Files with YYYYMMDD Date Format
Question details
The user wants to use a VBA macro to rename files by prepending the creation date and specific text, but encounters issues because standard date formats include invalid characters for Windows file names.

- Product
- Excel
- Device & OS
- Windows
- Scenario
- Automating file renaming processes using a VBA macro to include the current or creation date in the file name.
- Observed behavior
- The macro fails or generates errors when the default date format inserts prohibited characters (like slashes) into the Windows filename.
Ensure you have a backup of the files you intend to rename, and confirm that the Developer tab is enabled in your spreadsheet program to access the VBA editor.
Format the Date as YYYYMMDD in Your VBA Macro
Modify the VBA Date format to ensure the resulting text string contains no prohibited characters, allowing for valid Windows filenames.
Windows operating systems strictly prohibit the use of certain characters in filenames, including slashes (/), colons (:), asterisks (*), and question marks (?).
When using VBA to append dates to filenames, the default date format often includes slashes (e.g., yyyy/mm/dd), which will cause the renaming macro to fail. By utilizing the VBA.Format function, you can strip these illegal characters and safely append the date.
Press Alt + F11 in your spreadsheet program to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer panel, find and open the module that contains your file renaming macro.
Find the line where the date is generated and modify it to format the date without slashes. Update the code to: X = VBA.Format(Date, "yyyymmdd").
Combine the newly formatted date with your custom text and the original file name string. For example: NewFileName = X & "_Insert_new_Info-" & OriginalFileName.
Save your script and press F5 or click the Run button at the top of the VBA editor window to execute the macro and safely rename your files.

Automate File Renaming with WPS Spreadsheet VBA
WPS Office offers robust VBA macro capabilities out of the box, allowing you to easily automate repetitive tasks like batch renaming files with custom date formatting.
- 1. Enable the Developer Tab: Open WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled in your ribbon.
- 2. Launch the VBA Editor: Click the Developer tab on the top ribbon, then click on the 'VBA Editor' icon to open the coding environment.
- 3. Write or Paste Your Macro: Insert a new module and paste your file renaming script using the VBA.Format(Date, "yyyymmdd") method to ensure compliance with Windows naming conventions.
- 4. Run the Automation: Save your workbook as a Macro-Enabled Spreadsheet (.xlsm) and click 'Run' to seamlessly batch rename your target files.

Frequently Asked Questions
Why do I get a 'Bad file name or number' error when running my macro?
This error typically occurs when your VBA code attempts to save or rename a file using prohibited characters. Windows does not allow slashes (/), colons (:), or angle brackets (<>) in filenames, which are often unintentionally included when generating default date strings.
How do I extract the actual file creation date instead of today's date?
To get the creation date of an existing file rather than the current date, you can use the FileDateTime(FilePath) function in VBA. You can then wrap this function in the formatting method, like this: VBA.Format(FileDateTime(FilePath), "yyyymmdd").
Will these macros work if I share my spreadsheet with others?
Yes, as long as you save the workbook in a macro-enabled format (.xlsm) and the recipient's spreadsheet software supports VBA (such as Excel or WPS Office). The recipient must also enable macro execution in their security settings.




