Excel Guides

How to force a workbook to close after inactivity in Excel

In Excel, you can force a workbook to close after a period of inactivity by using the Workbook Close event. This event occurs when the workbook is closed, either through the File menu or by clicking the Close button on the workbook window. You can use this event to check for inactive periods and then close the workbook if desired.

To use the Workbook Close event:

  1. Open the workbook in which you want to check for inactivity.
  2. Press Alt+F11 to open the Visual Basic Editor.
  3. In the Project Explorer window, double-click ThisWorkbook.
  4. In the Code window, click Workbook in the Object drop-down list at the top of the window.
  5. Click Workbook_Close in the Procedure drop-down list.
  6. Enter your code in the Code window.


Private Sub Workbook_Close() 

   'Check for inactivity 

   If Application.ActiveWorkbook Is Nothing Then 

      'No activity for 5 minutes 

      If Now - ThisWorkbook.SavedTime > TimeValue("00:05:00") Then 

         'Save changes and close workbook 


         ThisWorkbook.Close False 

      End If 

   End If 
End Sub

Move beyond 


Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.