Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
ResetCount
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
ResetCount
End Sub
Option Explicit
Public gCount As Long
Public Const kLIMIT = 30 '30 SEC LIMIT
Sub Timer()
Dim tTime
tTime = Now + TimeValue("00:00:01")
Application.OnTime tTime, "ClickTimer"
End Sub
Sub ClickTimer()
Dim tTime
tTime = tTime - TimeSerial(0, 0, 1)
gCount = gCount + 1
Debug.Print gCount
If gCount > kLIMIT Then
'MsgBox "Countdown complete."
ActiveWorkbook.Close True
Exit Sub
End If
Call Timer
End Sub
Public Sub ResetCount()
gCount = 0
End Sub
Public Sub StartTimer()
ResetCount
ClickTimer
End Sub
how about if it should close at let's say 3 minutes of inactivity?? because I tried modifying your code by just changing 30 to 180 but it doesn't work. hoping for your responsewow, 30 seconds is not much time....
open the VBE window: Alt-F11,
in the far left PROJECT window pane,
under the VBAProject (your workbook)
dbl-click the ThisWorkbook object,
PASTE THE FOLLOWING CODE:
Code:Private Sub Workbook_Open() StartTimer End Sub Private Sub Workbook_SheetActivate(ByVal Sh As Object) ResetCount End Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) ResetCount End Sub
then in a MODULE, paste this code:
Code:Option Explicit Public gCount As Long Public Const kLIMIT = 30 '30 SEC LIMIT Sub Timer() Dim tTime tTime = Now + TimeValue("00:00:01") Application.OnTime tTime, "ClickTimer" End Sub Sub ClickTimer() Dim tTime tTime = tTime - TimeSerial(0, 0, 1) gCount = gCount + 1 Debug.Print gCount If gCount > kLIMIT Then 'MsgBox "Countdown complete." ActiveWorkbook.Close True Exit Sub End If Call Timer End Sub Public Sub ResetCount() gCount = 0 End Sub Public Sub StartTimer() ResetCount ClickTimer End Sub
When the workbook opens, it starts counting to kLIMIT.
when a user types , it resets to 0.
Put this code in the ThisWorkbook module:I want to autoclose excel after 20 minutes but only if workbook is in read only mode . I also want counting to work even if minimized .
Option Explicit
Private Sub Workbook_Open()
If Me.ReadOnly Then StartTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Me.ReadOnly Then StopTimer
End Sub
Option Explicit
Public RunWhen As Double
Public Const cRunWhat = "CloseWorkbook"
Public Sub StartTimer()
RunWhen = Now + TimeValue("00:20:00")
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
End Sub
Public Sub CloseWorkbook()
If Workbooks.Count > 1 Then
'More than 1 workbook is open so close only this workbook
ThisWorkbook.Close SaveChanges:=False
Else
'Only 1 workbook is open so close the Excel app, which also closes this workbook
Application.Quit
End If
End Sub
Public Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=False
End Sub