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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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