Solved

Excel workbook auto save/close after inactivity

Posted on 2012-03-15
8
1,302 Views
Last Modified: 2012-03-16
Hello:

I would like a macro that runs in the back ground in excel that would save the workbook and close it after a given time frame if there was no activity by the user.
0
Comment
Question by:DavidH7470
8 Comments
 
LVL 2

Expert Comment

by:costaf
ID: 37725251
hi, there. if i´m not mistaken there is an option in Excel 2010 Save options that allows you to auto save  every x minutes , and it saves your workbook automaticly , even if you close the document without saving it.
0
 
LVL 20

Accepted Solution

by:
netcmh earned 500 total points
ID: 37725252
0
 
LVL 15

Expert Comment

by:jerseysam
ID: 37725261
Dim DownTime As Date


Sub SetTime()
DownTime = Now + TimeValue("00:00:20")
Application.OnTime DownTime, "ShutDown"
End Sub


Sub ShutDown()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub


Sub Disable()

On Error Resume Next
Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", _
Schedule:=False
End Sub
0
 

Author Comment

by:DavidH7470
ID: 37725352
Hi Jerseysam

That works to a point.  But it closes even as a user is entering information.  I was looking for it close it after an interval of inactivity.

Thanks
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 20

Expert Comment

by:netcmh
ID: 37725370
The link I provided has the inactivity close segment.

Private Sub Workbook_Open()
MsgBox "This workbook will auto-close after 20 seconds of inactivity"
Call SetTime
End Sub


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


Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call Disable
Call SetTime
End Sub


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target _
As Excel.Range)
Call Disable
Call SetTime
End Sub
0
 
LVL 20

Expert Comment

by:netcmh
ID: 37725372
the module would have:

Dim DownTime As Date


Sub SetTime()
DownTime = Now + TimeValue("00:00:20")
Application.OnTime DownTime, "ShutDown"
End Sub


Sub ShutDown()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub


Sub Disable()

On Error Resume Next
Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", _
Schedule:=False
End Sub
0
 
LVL 3

Expert Comment

by:DaFranker
ID: 37725455
To use Jerseysam's solution, add this code in ThisWorkbook:

Private Sub Workbook_Open()
    'Remove the single-quote at the start of the line below to give a warning to the user.
    'MsgBox "This workbook will auto-close after 20 seconds of inactivity"
    Call SetTime
End Sub


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


Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call Disable
    Call SetTime
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)
    Call Disable
    Call SetTime
End Sub


Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target _
As Excel.Range)
    Call Disable
    Call SetTime
End Sub

Open in new window

0
 
LVL 20

Expert Comment

by:netcmh
ID: 37729061
Thank you for the grade. Good luck.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now