Auto Close, after X minutes-code not working

froggiebeckie

Board Regular
Joined
Sep 27, 2002
Messages
87
I've got a couple of files posted on a shared drive, so multiple folks can access them.

One (Schedule) has code to save and close after 30 minutes, since folks are prone to open and then walk away from their desks.
It works fine.

But, when I tried to use the same code on the second file (MDL), it doesn't work.

Both are .xlsm files
Can anyone suggest what the issue may be?

Thanks,
BeckieO

Here are the codes for the 2 files.

MDL
'PLACE THIS CODE IN MODULE 1:
'******************************

Dim BootTime As Date

Sub StartTime()


'Start the Timer
BootTime = Now + TimeValue("00:30:10") 'Set your time here
Application.OnTime BootTime, "ShutDown"
End Sub

Sub ShutDown()


'At this point the main timer has elapsed.
Application.WindowState = xlMaximized 'Maximize window
Application.DisplayAlerts = False 'suppresses all messages about saving
ActiveWorkbook.Close savechanges:=True 'FALSE = changes are not saved

ThisWorkbook.Close
End Sub


Sub Disable()


'Stop the Timer
On Error Resume Next
Application.OnTime EarliestTime:=BootTime, _
Procedure:="ShutDown", Schedule:=False
End Sub


CLASS 1

Private Sub workbook_open()

End Sub
'start timer
Call StartTime

Private Sub workbook_beforeclose(canecel As Boolean)
'Stop the timer
Call Disable

End Sub









SCHED
'PLACE THIS CODE IN MODULE 1:
'******************************

Dim BootTime As Date

Sub StartTime()


'Start the Timer
BootTime = Now + TimeValue("00:30:10") 'Set your time here
Application.OnTime BootTime, "ShutDown"
End Sub

Sub ShutDown()


'At this point the main timer has elapsed.
Application.WindowState = xlMaximized 'Maximize window
Application.DisplayAlerts = False 'suppresses all messages about saving
ActiveWorkbook.Close savechanges:=True 'FALSE = changes are not saved

ThisWorkbook.Close
End Sub


Sub Disable()


'Stop the Timer
On Error Resume Next
Application.OnTime EarliestTime:=BootTime, _
Procedure:="ShutDown", Schedule:=False
End Sub


CLASS 1
Private Sub workbook_open()

End Sub
'start timer
Call StartTime

Private Sub workbook_beforeclose(canecel As Boolean)
'Stop the timer
Call Disable

End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
OK, never mind.
Found some simpler code that works fine, so I'll just chalk this one up to the Magic of Excel.

Thanks anyway.
Here's what worked:

ALT+F11 to open VB editor. Double click 'ThisWorkbook' and paste the first 2 modules in on the right.


Private Sub Workbook_Open()
StartTimer
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
StartTimer
End Sub


Now right click 'ThisWorkbook' insert module and paste this code in. Save close and re-open the workbook and after 15 minutes of idle time it will save and close automatically.


Const idleTime = 900 'seconds
Dim Start
Sub StartTimer()
Start = Timer
Do While Timer < Start + idleTime
DoEvents
Loop
Application.DisplayAlerts = False
ActiveWorkbook.Close True
Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top