Inactivity timer re-opens workbook

gmoney2996

New Member
Joined
May 29, 2019
Messages
1
Hello,

First time poster so I apologize if I break any sort of rules.

I have a workbook which has a timer that will kick out the user if the worksheet doesn't change for 30 minutes.

The timer works fine if I have no other instances of excel open. But, as soon as I have another workbook open and I try to close the workbook with the timer, the workbook closes but then reopens right away.

I've found a few ideas that would suggest I'm missing something in the "Workbook_BeforeClose" section, I just cannot figure out what. Any help would be much appreciated. Thanks!

In the workbook:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

End Sub

Private Sub Workbook_Open()
MsgBox ("This Workbook has a 30 minute inactivity timer.")
Call StartTimer
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call ResetCount
End Sub

In module:
Code:
Option Explicit
Public StopTime As Date
Public gCount As Long
Public Const kLIMIT = 1800  '***30 MINUTE LIMIT***

Sub Timer()
Dim tTime
    tTime = Now + TimeValue("00:00:01")
    Application.OnTime tTime, "ClickTimer"
End Sub

Sub ClickTimer()
Dim tTime
Dim time_remaining As Integer

tTime = tTime - TimeSerial(0, 0, 1)
gCount = gCount + 1
ThisWorkbook.Worksheets("PTE INPUT").Range("S6").Value = Format(((kLIMIT - gCount) / 60), "00.00")
If gCount > kLIMIT Then
    'MsgBox "Countdown complete."
    Application.StatusBar = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close True
    Exit Sub
End If
Call Timer
End Sub

Public Sub ResetCount()
gCount = 0
StopTime = Now + TimeValue("00:00:01")
End Sub

Public Sub StartTimer()
Call ResetCount
Call ClickTimer
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Untested and I haven't looked closely at your code.

In the module, delete this line in Sub Timer:
Code:
Dim tTime
Add this line above all procedures:
Code:
Public tTime As Date
Add this:
Code:
Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=tTime, Procedure:="ClickTimer", Schedule:=False
End Sub
Add this to the ThisWorkbook module:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
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