Solved

Excel workbook auto save/close after inactivity

Posted on 2012-03-15
8
1,333 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

911 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

19 Experts available now in Live!

Get 1:1 Help Now