How to Automatically Delete Old Excel VBA Backup Files
Question details
The user wants to automatically remove old backup copies of an Excel workbook generated by a VBA Workbook_BeforeSave event without accidentally deleting the only recent copy.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating the cleanup of backup files in a subfolder using VBA macros.
- Observed behavior
- Timestamped backups accumulate in a subfolder, and blindly deleting old files by time risks removing the most recent usable backup if multiple saves occur rapidly.
Before modifying your VBA macro code, manually copy your current workbook to a safe location to ensure you do not lose data if the new script overwrites files unexpectedly.
Use Date-Based Naming to Manage Backups
Instead of writing complex logic to delete old files, modify your backup filename to include only the date. This naturally overwrites older backups from the same day, leaving exactly one backup per day.
When you include hours, minutes, and seconds in your backup filename, every save creates a new file. By removing the time and using only the date, saving multiple times on the same day simply overwrites the day's existing backup.
This ensures you always have the most recent backup for any given day without writing risky file deletion scripts.
Press Alt + F11 in your Excel workbook to open the Visual Basic for Applications editor.
In the Project Explorer, double-click on 'ThisWorkbook' to view your Workbook_BeforeSave event code.
Find the line where the backup filename is generated. Change the timestamp format from Format(Now, "yyyy-mm-dd hh-mm-ss") to Format(Now, "mmm dd yyyy").
Save your VBA code and trigger a workbook save. Verify that the subfolder now contains a single backup file for the current date.

Run and Manage Excel VBA Macros in WPS Office
WPS Office provides robust support for VBA macros, allowing you to seamlessly run, edit, and troubleshoot your Workbook_BeforeSave events and automated backup scripts.
- 1. Download WPS Office: Install the free version of WPS Office from the official website.
- 2. Enable the Developer Tab: Open WPS Spreadsheet, go to the settings, and enable the Developer tools to access macro features.
- 3. Open Your Macro Workbook: Open your macro-enabled workbook (.xlsm). WPS Office will automatically recognize and allow you to edit your VBA scripts.
- 4. Run VBA Scripts: Click on 'Macros' or 'Visual Basic' in the Developer tab to edit your automatic backup code easily.

Frequently Asked Questions
Why shouldn't I write a VBA script to delete backups older than a specific time?
Deleting files strictly by age (e.g., older than 2 hours) is risky. If you save your workbook multiple times in a short period and the script deletes older files, a subsequent error could leave you without any functional recent backups.
How can I keep exactly one backup per month using VBA?
Modify the filename string in your VBA code to use a month and year format, such as Format(Now, "mmm yyyy"). Every save during that month will overwrite the same backup file.
What happens if I remove the time from the filename but my macro throws a debugging error?
Ensure that your updated filename string does not contain illegal characters (like slashes or colons) and properly concatenates with the file extension (e.g., ".xlsm"). Check your syntax around the Format function.




