ThisCalculator

Everyday

Auto Close Excel After Idle Time: How to Set It Up

Updated · Standard formula, shown below

Auto Close Excel After Idle Time: How to Set It Up

Yes, you can make Excel close automatically after a set period of inactivity, and the most reliable way is a small VBA macro that runs when the workbook opens, starts a timer, and saves and closes the file if no key is pressed or cell is edited within the idle limit. This article explains the exact code, how to install it, the built-in alternatives, and the common pitfalls to avoid.

Why You Might Want Auto-Close in Excel

Leaving Excel open consumes memory and can leave sensitive data visible on screen. Auto-closing after idle time is useful for shared computers, public kiosks, training labs, or when you frequently forget to save and close files before stepping away. It also reduces the risk of another person editing or viewing your work when you are not at your desk.

Excel itself does not have a native “close after idle� setting. The feature must be added through a macro or by using Windows-level tools. The macro approach is the most direct because it works entirely inside Excel and can be saved with the workbook so it travels with the file.

How the VBA Idle Timer Works

The core idea is to use Excel’s Application.OnTime method, which schedules a procedure to run after a specified number of seconds. The macro records the last time the user interacted with the workbook—by pressing a key, clicking a cell, or changing a value—and compares it to the current time. If the difference exceeds your idle limit, the macro saves and closes the workbook.

You need two procedures: one that starts the timer when the workbook opens, and another that checks the idle time and either resets the timer or closes the file. The check runs repeatedly, typically every 30 or 60 seconds, so the close happens within that interval after the idle limit is reached.

Step-by-Step: Add the Auto-Close Macro

  1. Press Alt + F11 to open the VBA editor.
  2. In the Project Explorer (left pane), double-click ThisWorkbook.
  3. Paste the following code into the window:
Private Sub Workbook_Open()
    StartIdleTimer
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    On Error Resume Next
    Application.OnTime EarliestTime:=NextCheck, Procedure:="CheckIdle", Schedule:=False
End Sub

Public NextCheck As Date

Sub StartIdleTimer()
    NextCheck = Now + TimeValue("00:01:00")
    Application.OnTime EarliestTime:=NextCheck, Procedure:="CheckIdle"
End Sub

Sub CheckIdle()
    Dim IdleMinutes As Long
    IdleMinutes = 5  ' change to your desired idle time in minutes
    If Now - LastAction > TimeValue("00:" & IdleMinutes & ":00") Then
        ThisWorkbook.Save
        ThisWorkbook.Close
    Else
        NextCheck = Now + TimeValue("00:01:00")
        Application.OnTime EarliestTime:=NextCheck, Procedure:="CheckIdle"
    End If
End Sub

This code uses a variable LastAction that you must update whenever the user interacts. Add this to a normal module:

Public LastAction As Date

Sub ResetIdle()
    LastAction = Now
End Sub

Then, in the ThisWorkbook code, add event handlers for Workbook_SheetSelectionChange and Workbook_SheetChange that call ResetIdle:

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    ResetIdle
End Sub

Private Sub Workbook_SheetChange(ByVal Target As Range)
    ResetIdle
End Sub

Finally, save the file as a macro-enabled workbook (.xlsm). The timer starts when the file opens and checks every minute. If no selection change or cell edit occurs for the idle minutes you set, the workbook saves and closes.

Choosing the Right Idle Time and Check Interval

Set the idle time based on how long a user might legitimately pause. For a shared kiosk, 2–3 minutes is common. For a personal workstation, 10–15 minutes avoids closing while you are reading a long document or thinking. The check interval (the 1-minute value in the code) determines how promptly the close happens after the idle limit. A shorter interval (30 seconds) closes faster but runs the macro more often, which uses a small amount of CPU.

If you want the workbook to close without saving, remove the ThisWorkbook.Save line. If you want to prompt the user before closing, replace the save/close lines with a message box that asks for confirmation.

Common Problems and How to Fix Them

  • Macro does not run: Ensure macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and choose “Enable all macrosâ€� (only for files you trust).
  • Timer does not reset on mouse clicks: The code only resets on selection changes and cell edits. If you want mouse clicks without selection changes to count as activity, you need a Windows API hook, which is more complex and beyond the scope of this article.
  • Workbook closes while user is typing in a cell: The Workbook_SheetChange event fires after the edit is committed (Enter or Tab). If the user types slowly, the timer may close mid-typing. To avoid this, increase the idle time or use a shorter check interval.
  • Timer keeps running after closing: The Workbook_BeforeClose event cancels the scheduled check. If you close via the X button, it works. If you force-close via Task Manager, the timer may persist until Excel fully exits.
  • File is read-only or shared: ThisWorkbook.Save may fail. Use ThisWorkbook.SaveCopyAs or handle errors gracefully.

Alternatives to VBA: Windows Task Scheduler and Third-Party Tools

If you cannot use macros (e.g., your organization disables them), you can use Windows Task Scheduler to close Excel after a set time, but it cannot detect idle time—it will close Excel regardless of activity. A better alternative is a third-party utility that monitors keyboard and mouse input and sends a close command to Excel after a period of inactivity. These tools are not built into Windows and require installation. Another option is to use Excel’s built-in AutoRecover and AutoSave features to minimize data loss, then manually close the file when you leave—though that does not automate the close.

Safety and Data Integrity Considerations

Auto-closing with save is generally safe, but it can overwrite changes you intended to discard. If you are experimenting or want to review edits, disable auto-save or set the macro to close without saving. Also, if the workbook contains external links or macros that take time to update, the save may fail or be slow. Always test the macro on a copy of your file first. The Application.OnTime method is not persistent across Excel restarts—it only works while the workbook is open. If Excel crashes, the timer is lost, and the file remains open until you close it manually.

Frequently Asked Questions

Can I set a different idle time for each workbook?

Yes, because the macro is stored in each workbook. Change the IdleMinutes value in the CheckIdle procedure for that specific file. If you want a global setting, you would need to put the macro in your Personal Macro Workbook, but then it applies to all open workbooks.

Will the macro close Excel entirely or just the workbook?

The code closes only the workbook in which the macro resides. If other workbooks are open, Excel remains running. To close Excel entirely, use Application.Quit instead of ThisWorkbook.Close.

Does the timer reset when I scroll or use the mouse wheel?

No, scrolling does not trigger the SheetSelectionChange or SheetChange events. Only selecting a different cell or editing a cell resets the timer. If you need scroll to count as activity, you would need a more advanced API-based solution.

Can I make it prompt before closing?

Yes, replace the ThisWorkbook.Save and ThisWorkbook.Close lines with a MsgBox that asks “Close now?� and then conditionally saves and closes. This prevents accidental data loss.

Will this work in Excel for Mac?

Yes, VBA is available in Excel for Mac, but the Application.OnTime method works slightly differently. The code should run, but test it on your Mac version. Some Windows-specific API calls will not work, but the basic timer does.

What if the user is actively typing but not changing cells?

If the user is typing in a cell but has not pressed Enter, the SheetChange event does not fire. The timer may close the workbook mid-edit. To prevent this, you can use the Worksheet_Change event on each sheet, but it still only fires after the edit is committed. The only robust solution is to track the KeyDown event, which requires a Windows API hook.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.