VBA & Macro Problems

How to Use VBA to Rename Files with YYYYMMDD Date Format

Emma BrownEmma Brown Oct 26, 2026 868 views

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.

How to Use VBA to Rename Files with YYYYMMDD Date Format
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 in your spreadsheet program to open the Visual Basic for Applications (VBA) editor.

2
Locate the Renaming Macro

In the Project Explorer panel, find and open the module that contains your file renaming macro.

3
Format the Date Variable

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").

4
Concatenate the New Filename

Combine the newly formatted date with your custom text and the original file name string. For example: NewFileName = X & "_Insert_new_Info-" & OriginalFileName.

5
Execute the Code

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.

Format the Date as YYYYMMDD in Your VBA Macro
Alternative Valid Formats: If you prefer separators in your date format for better readability, you can safely use hyphens (-), as they are permitted in Windows filenames. You would format it as VBA.Format(Date, "yyyy-mm-dd").
WPS Advanced Features

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. 1. Enable the Developer Tab: Open WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled in your ribbon.
  2. 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. 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. 4. Run the Automation: Save your workbook as a Macro-Enabled Spreadsheet (.xlsm) and click 'Run' to seamlessly batch rename your target files.
Fully compatible with Microsoft Excel VBA macros and .xlsm formats.Built-in VBA editor for quick scripting, debugging, and execution.Lightweight installation with fast processing speeds for large datasets.
microsoft office alternative - wps office

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.