How to Automatically Save and Close an Excel Workbook After 30 Minutes
Question details
The user needs to implement a scheduled timer in an Excel workbook that displays a countdown, saves the file automatically, and closes it after exactly 30 minutes.

- Product
- Microsoft Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Managing shared or inactive workbooks where a file needs to be automatically saved and closed to free up access for other users or ensure data is periodically saved.
- Observed behavior
- By default, workbooks remain open indefinitely until manually saved and closed. A VBA solution is required to enforce a strict 30-minute activity window with a visual countdown.
Before adding VBA code to your workbook, save a backup copy of your file. Macro actions cannot be undone, and automatically closing a workbook could result in data loss if other unsaved files are unintentionally affected.
Use a VBA Macro to Implement a 30-Minute Timer, Save, and Close
You can achieve this by writing a custom VBA script that tracks time, displays a countdown in a specific cell (like A1), and executes the save and close commands when the time expires.
This solution relies on the Application.OnTime method in VBA, which schedules a specific macro to run at a defined future time.
To make this work seamlessly, you will need a standard module for the countdown logic and a Workbook_Open event to trigger the timer as soon as the file is opened.
Press Alt + F11 on your keyboard while the Excel workbook is open to launch the Visual Basic for Applications (VBA) editor.
In the VBA editor, click 'Insert' from the top menu and select 'Module'. This is where you will define the timer logic.
In the new module, create a subroutine using Application.OnTime to schedule the countdown interval. The script should update cell A1 with the remaining time and, upon reaching zero, execute 'ThisWorkbook.Save' followed by 'ThisWorkbook.Close'.
In the Project Explorer pane on the left, double-click 'ThisWorkbook'. Choose 'Workbook' from the left dropdown and 'Open' from the right dropdown. Insert the command to call your timer subroutine so it starts automatically when the file opens.
Close the VBA editor and save your file. You must select 'Excel Macro-Enabled Workbook (*.xlsm)' from the 'Save as type' dropdown to ensure your code is preserved.

Manage Macros and Automate Tasks Easily with WPS Spreadsheet
WPS Office provides robust support for Microsoft Excel formats, including macro-enabled workbooks (.xlsm). You can easily run your automated save-and-close scripts using WPS Spreadsheet's built-in developer tools.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your .xlsm file using the WPS Spreadsheet application.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon. If it is not visible, you can enable it in the application settings.
- 3. Open the Script Editor: Click on 'Macros' or 'Visual Basic' to open the built-in VBA editor and paste your countdown and auto-close script.
- 4. Run Your Automation: Save your code and run the macro. Your file will now automatically execute the 30-minute countdown and safely save your progress.

Frequently Asked Questions
Can I change the timer to 15 minutes instead of 30 minutes?
Yes, in your VBA script, you can easily adjust the TimeValue parameter. Change '00:30:00' to '00:15:00' to trigger the automatic save and close action after exactly 15 minutes.
Will this macro accidentally close other open Excel files?
If you strictly use 'ThisWorkbook.Close' in your code, it will only close the specific file containing the macro. Be careful to avoid using 'Application.Quit', as that command will forcefully close the entire Excel application and all other open files.
Why doesn't my macro run automatically when I open the workbook?
For the macro to start automatically, the initialization code must be placed inside the 'Workbook_Open' event within the 'ThisWorkbook' module. Additionally, you must ensure that macros are enabled in your Trust Center or security settings when the file is opened.
Is it possible to cancel the timer once it has started?
Yes, you can create a separate macro that uses the 'Application.OnTime' method combined with the 'Schedule:=False' parameter. This will cancel the pending operation and stop the workbook from closing.




