Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to automatically save and close an Excel file after a certain idle time?

In some cases, such as leaving an Excel file on a shared disk, when the file is still opening by a user, others are unable to edit the workbook. So how to auto close the file after a certain idle time in order to ease other’s work? This article will help you with VBA method.

Auto save and close an Excel file after a certain idle time with VBA code

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

Auto save and close an Excel file after a certain idle time with VBA code


To auto save and close an Excel file after a certain idle time, please do as follows.

1. Open the workbook you need to make it auto saved and closed after a certain idle time. Then press the Alt + F11 keys together to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, please double click the ThisWorkbook in the right pane to open the Code window. Then copy and paste below VBA code into the Code window. See screenshot:

VBA code 1: Auto save and close an Excel file after a certain idle time

Dim xTime As String
Dim xWB As Workbook

Private Sub Workbook_Open()
'Updated by Extendoffice 2019/1/20
    On Error Resume Next
    xTime = Application.InputBox("Please specify the idle time:", "KuTool For Excel", "00:00:20", , , , , 2)
    Set xWB = ActiveWorkbook
    If xTime = "" Then Exit Sub
    Reset
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    On Error Resume Next
    If xTime = "" Then Exit Sub
    Reset
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
    If xTime = "" Then Exit Sub
    Reset
End Sub

Sub Reset()
    Static xCloseTime
    If xCloseTime <> 0 Then
        ActiveWorkbook.Application.OnTime xCloseTime, "SaveWork1", , False
    End If
    xCloseTime = Now + TimeValue(xTime)
    ActiveWorkbook.Application.OnTime xCloseTime, "SaveWork1", , True
End Sub

3. Then go on clicking Insert > module, and copy and paste below code into the Module window. See screenshot:

VBA code 2: Auto save and close an Excel file after a certain idle time

Sub SaveWork1()
'Updated by Extendoffice 2019/1/20
    Application.DisplayAlerts = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    
    Application.DisplayAlerts = True
End Sub

4. Press the Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window.

5. Click File > Save As > Browse. See screenshot:

6. In the Save As dialog box, please select a folder to save the file, name it as you need in the File name box, select Excel Macro-Enabled Workbook from the Save as type drop-down list, and finally click the Save button. See screenshot:

From now on, every time when opening this workbook, a Kutools for Excel dialog box will pop up. Please enter the time you will save and close the workbook based on, and then click the OK button.

And the workbook will be saved and closed automatically after that specified idle time. See screenshot:


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Nitin · 23 days ago
    it is asking for idle time everytime I am opening.

    I want time should not be shown while opening the workbook. (if required, it can be changed in VBA)
    • To post as a guest, your comment is unpublished.
      Tarun · 11 days ago
      Change line
      xTime = Application.InputBox("Please specify the idle time:", "KuTool For Excel", "00:00:20", , , , , 2)


      to

      xTime = "00:00:20"


      it will not ask
  • To post as a guest, your comment is unpublished.
    robert · 8 months ago
    Is there a way to make this work so even if you not on the sheet like click over to another window the timmer will still close that sheet? It seems like i have to stay on the file for it to work
  • To post as a guest, your comment is unpublished.
    Mark · 9 months ago
    I would recommend identifying the workbook with the workbook file name. This would allow the users to have multiple workbooks open and closing out only the specific workbook that needs to have the time lock.

    Suggestion: Set xwb = Workbooks("<file name>")
    instead of: Set xwb = ActiveWorkbook

    Also, replace all references to "ActiveWorkbook" to the Xwb in the module

    Great code otherwise.
    • To post as a guest, your comment is unpublished.
      Sammy · 6 months ago
      Hi Mark,

      Thanks for posting this amendment - found so many codes for closing inactive workbooks, but all of them seemed to affect the one you don't want to close!

      I'm very new to VBA, so I'm not sure how the code would work to replace the wording 'ActiveWorkbook' within the module. I've tried to replace the wording with 'Xwb' but that doesn't change anything and I may be being daft. Would you mind posting a copy of the code please?

      Thanks
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi Mark,
      Thank you for sharing.
  • To post as a guest, your comment is unpublished.
    VR · 10 months ago
    This does not work when focus in on a particular cell in a sheet. Use case is when user is typing in a cell and leave the excel open with the cursor inside the cell.
    • To post as a guest, your comment is unpublished.
      Rehan · 9 months ago
      Were you able to find a fix for this by any chance?
  • To post as a guest, your comment is unpublished.
    henry · 1 years ago
    Hi Thanks loads for this code. Just one issue. It seems to try to close all open workbooks thus stalls if another workbook close is cancelled or not responded to. have you any touch-ups that can fix this?
    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi Henry,
      The code has been updated with the problem solving. Please have a try. Sorry for the inconvenience and thank you for your comment.
    • To post as a guest, your comment is unpublished.
      Gaston · 1 years ago
      I have the same issue... I tried to open a new workbook, and entered some data.... when the time came, both workbooks were closed, but the new one wasn´t saved... Is there a way to fix this?
      Besides that, it works great!! Thanks a lot!!
      • To post as a guest, your comment is unpublished.
        crystal · 10 months ago
        Hi Gaston,
        The code has been updated with the problem solving. Please have a try. Sorry for the inconvenience and thank you for your comment.