Solved

Excel workbook auto save/close after inactivity

Posted on 2012-03-15
8
1,403 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

856 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