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 save and close workbook after inactivity for a certain amount of time?

In some times, you may accidently close a workbook when you are busy with other affairs for a long time which may lose some important data in the workbook. Is there any tricks to automatically save and close the workbook if you have inactivated it for a certain amount of time?

Auto save and close workbook after inactivity for a certain amount of time with VBA


arrow blue right bubble Auto save and close workbook after inactivity for a certain amount of time with VBA

There is no built-in function in Excel to solve this problem, but I can introduce a macro code which can help you to save and close workbook after inactivity in a certain time.

1. Enable the workbook you want to automatically save and close after inactivity for a certain seconds, and press Alt + F11 keys to open Microsoft Visual Basic for Applications window.

2. Click Insert > Module to create a Module script, and paste below code to it. See screenshot:

Dim CloseTime As Date
Sub TimeSetting()
    CloseTime = Now + TimeValue("00:00:15")
    On Error Resume Next
    Application.OnTime EarliestTime:=CloseTime, _
      Procedure:="SavedAndClose", Schedule:=True
End Sub
Sub TimeStop()
    On Error Resume Next
    Application.OnTime EarliestTime:=CloseTime, _
      Procedure:="SavedAndClose", Schedule:=False
 End Sub
Sub SavedAndClose()
    ActiveWorkbook.Close Savechanges:=True
End Sub

 

doc save close workbook after inactivity 1

3. Then in the Project Explorer pane, double click This Workbook, and paste below code to the beside script. See screenshot:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call TimeStop
End Sub

Private Sub Workbook_Open()
    Call TimeSetting
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
   Call TimeStop
   Call TimeSetting
End Sub

 

doc save close workbook after inactivity 2

4. Go to double click at the module you inserted in step 2, and press F5 key to run the code. See screenshot:
doc save close workbook after inactivity 3

5. Then after 15 seconds, there is a dialog popping out for remind you saving the workbook, and click Yes to save and close the workbook.
doc save close workbook after inactivity 4

Tips:

(1) In the first code, you can change the inactivity time to other in this string: Now + TimeValue("00:00:15")

(2) If you have never saved the workbook before, the Save As dialog box will come out firstly and ask you to save it.
doc save close workbook after inactivity 5


good Protect Worksheet

Kutools for Excel's Protect Worksheet function can quickly protect multiple sheets or the whole workbook at once.
doc protect multiple worksheets

Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
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.
    Rajesh rana · 9 days ago
    hi i want insert this code to an other code like expiration code with this code how i can do....?
    code is...following
    Private Sub Workbook_Open()

    Dim exdate As Date
    Dim i As Integer

    'modify values for expiration date here !!!
    anul = 2019 'year
    luna = 5 'month
    ziua = 16 'day

    exdate = DateSerial(anul, luna, ziua)

    If Date > exdate Then
    MsgBox ("The application " & ThisWorkbook.Name & " has expired !" & vbNewLine & vbNewLine _
    & "Expiration set up date is: " & exdate & " :)" & vbNewLine & vbNewLine _
    & "Contact Administrator to renew the version !"), vbCritical, ThisWorkbook.Name

    expired_file = ThisWorkbook.Path & "\" & ThisWorkbook.Name

    On Error GoTo ErrorHandler
    With Workbooks(ThisWorkbook.Name)
    If .Path <> "" Then

    .Saved = True
    .ChangeFileAccess xlReadOnly

    Kill expired_file

    'get the name of the addin if it is addin and unistall addin
    If Application.Version >= 12 Then
    i = 5
    Else: i = 4
    End If

    If Right(ThisWorkbook.Name, i) = ".xlam" Or Right(ThisWorkbook.Name, i) = ".xla" Then
    wbName = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - i)
    'uninstall addin if it is installed
    If AddIns(wbName).Installed Then
    AddIns(wbName).Installed = False
    End If
    End If

    .Close

    End If
    End With

    Exit Sub

    End If

    'MsgBox ("You have " & exdate - Date & "Days left")
    Exit Sub

    ErrorHandler:
    MsgBox "Fail to delete file.. "
    Exit Sub

    End Sub
  • To post as a guest, your comment is unpublished.
    seb · 3 months ago
    brilliant thanks
  • To post as a guest, your comment is unpublished.
    Torin · 3 months ago
    If you are working in a separate workbook at the point where close time is reached then it will close that workbook and not the inactive one. This can be solved by adjusting the code to:

    Dim CloseTime As Date
    Dim WKB As String
    Sub TimeSetting()
    WKB = ActiveWorkbook.Name
    CloseTime = Now + TimeValue("00:00:15")
    On Error Resume Next
    Application.OnTime EarliestTime:=CloseTime, _
    Procedure:="SavedAndClose", Schedule:=True
    End Sub
    Sub TimeStop()
    On Error Resume Next
    Application.OnTime EarliestTime:=CloseTime, _
    Procedure:="SavedAndClose", Schedule:=False
    End Sub
    Sub SavedAndClose()
    Workbooks(WKB).Close Savechanges:=True
    End Sub
  • To post as a guest, your comment is unpublished.
    Excel · 1 years ago
    The above code is not working when a cell is active. That is

    1. enter a value in the cell (don't press Enter or tab)

    2. minimize the excel.

    In this case the code is not working.