Close Workbook after 30 minutes automatically

eli_m

Board Regular
Joined
Jun 2, 2022
Messages
153
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am getting this error:
1675300137219.png


For:
1675300186583.png

Module Code:
VBA Code:
Option Explicit
Public Close_Time As Date
Sub start_Countdown()
    Close_Time = Now() + TimeValue("00:30:00")
    Application.OnTime Close_Time, "close_WB"
    End Sub
Sub stop_Countdown()
    Application.OnTime Close_Time, "close_WB", , False
    End Sub
Sub close_wb()
    ThisWorkbook.Save
If Application.Wait(Now + TimeValue("00:00:05")) = True Then
    MsgBox "Timed out after 30 minutes - Your work has been saved."
End If
    ThisWorkbook.Close
    End Sub


Workbook:

Top of code:
VBA Code:
Option Explicit
Private Sub Workbook_Open()
    start_Countdown

.........
Bottom of code:
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    stop_Countdown
    start_Countdown
    End Sub

I only want the workbook to close when there has been no SheetChanges in the last 30 minutes. I want the timer to restart every time there is a SheetChange. Right now it is closing at 30minutes from opening.

Can anyone see what the problem is?

Thanks in advance!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I think this idea would require two different modules being added, One to the 'ThisWorkbook' module and another to a regular module.

It appears that you want to save the workbook before closing it? Are you sure?
 
Upvote 0
I think this idea would require two different modules being added, One to the 'ThisWorkbook' module and another to a regular module.

It appears that you want to save the workbook before closing it? Are you sure?
Firstly, thanks for the reply!

I thought I had two different modules? One is running in the workbook and one as regular.

I do want to save the changes to the workbook before closing - I even have a 5-second pause to make sure the workbook saves. Is there something wrong with that? The document is shared and has autosave on. I pretty much just want to kick off inactive users
 
Upvote 0
What I was trying to get at is, if someone has made some changes that may not be desirable, and the time expires, the undesired changes will be saved.
 
Upvote 0
What I was trying to get at is, if someone has made some changes that may not be desirable, and the time expires, the undesired changes will be saved.
It's all on OneDrive so I can go back a version :) - Are you able to help with the first error? Its the module that is by itself (right now I've commented it out so I can continue using the document):
1675305433812.png
 
Upvote 0
You could try the following code:

In the 'ThisWorkbook' module, copy/paste the following:
VBA Code:
Private Sub Workbook_Open()
    Call SetTimer
End Sub

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

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call StopTimer
    Call SetTimer
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
    Call StopTimer
    Call SetTimer
End Sub

Then, in a regular module copy/paste the following:
VBA Code:
Dim DownTime As Date

Sub SetTimer()
    DownTime = Now + TimeValue("00:30:00")                                                      ' This sets the timer for 30 minutes of inactivity
    Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", Schedule:=True
End Sub

Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", Schedule:=False
 End Sub

Sub ShutDown()
'
    With ThisWorkbook
        Application.DisplayAlerts = False
        .Close Savechanges:=True
        Application.DisplayAlerts = True
    End With
End Sub
 
Upvote 0
Solution
You could try the following code:

In the 'ThisWorkbook' module, copy/paste the following:
VBA Code:
Private Sub Workbook_Open()
    Call SetTimer
End Sub

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

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call StopTimer
    Call SetTimer
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
    Call StopTimer
    Call SetTimer
End Sub

Then, in a regular module copy/paste the following:
VBA Code:
Dim DownTime As Date

Sub SetTimer()
    DownTime = Now + TimeValue("00:30:00")                                                      ' This sets the timer for 30 minutes of inactivity
    Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", Schedule:=True
End Sub

Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", Schedule:=False
 End Sub

Sub ShutDown()
'
    With ThisWorkbook
        Application.DisplayAlerts = False
        .Close Savechanges:=True
        Application.DisplayAlerts = True
    End With
End Sub

That seems to do the trick perfectly! Do you know where I can add in "MsgBox "Timed out after 30 minutes - Your work has been saved."" ?

I want that dialogue to pop up so they know they have been kicked. Thanks again!
 
Upvote 0
Place it in the 'Sub ShutDown'

VBA Code:
Sub ShutDown()
'
    With ThisWorkbook
        Application.DisplayAlerts = False
        .Close Savechanges:=True
        Application.DisplayAlerts = True
    End With
'
    MsgBox "Timed out after 30 minutes - Your work has been saved."
End Sub
 
Upvote 0
Place it in the 'Sub ShutDown'

VBA Code:
Sub ShutDown()
'
    With ThisWorkbook
        Application.DisplayAlerts = False
        .Close Savechanges:=True
        Application.DisplayAlerts = True
    End With
'
    MsgBox "Timed out after 30 minutes - Your work has been saved."
End Sub

Thank works now to - You're amazing.

I have actually moved the Code for the work book just on the sheet thats mostly used (only I use the other sheets). How can I make it so the timer doesn't run when I am on another sheet?

I tried:
VBA Code:
Private Sub Worksheet_Activate()
   
  Call StopTimer
  ThisWorkbook.RefreshAll ' Auto Refresh Pivot Tables
 
On Error Resume Next

End Sub

and it still kicked me out
 
Upvote 0
It sounds like you are trying to defeat the purpose of your original question. Wrap this thread up and start a new thread if you want to only monitor inactive time on a specific sheet.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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