WHAT IS DOES
The following code starts a timer when you goto any sheet exept for sheet1 and sheet2.
This timer will, if there is no sheet change for 30 seconds, trigger macro that will take you back to a defualt sheet eg. Sheet1
THINGS TO CONFIGURE
Public Const ExemptSheets As String = "Sheet1,Sheet2"
Public Const DeFaultSheet As String = "Sheet1"
Const TimeLag As Variant = "00:00:30"
ExemptSheets are sheets that you do not want the timer to run on , currently timer does not run when your on sheets named "Sheet1" or "Sheet2"
DeFaultSheet is the sheet that you want to go back to when timer run out , eg. currently set for "Sheet1"
TimeLag is the time to wait for user to change a sheet value hh:mm:ss , eg currently set at 30 seconds
CAUTION
Make sure you run "StopTimer" before you attempt to change anysettings. Or you could end up in a very nasty loop. In fact the best way to configure settings is to , 1. exit worksheet ,2. re-open worksheet with macros disabled , 3. make changes , 4. re-open book with macro's enabled.
PLACE THIS CODE IN A MODULE
Code:
Public dNext As Variant
Public Const ExemptSheets As String = "Sheet1,Sheet2"
Public Const DeFaultSheet As String = "Sheet1"
Const TimeLag As Variant = "00:00:30"
Public Sub StartTimerAgain()
'calculate when code should run again
dNext = Now + TimeValue(TimeLag)
'set on time event to trigger running
Application.OnTime dNext, "DoAction"
End Sub
Public Sub DoAction()
Sheets(DeFaultSheet).Select
'start timer again
Call StartTimerAgain
End Sub
Public Sub StopTimer()
' time has not been used yet if dNext empty
If IsEmpty(dNext) Then Exit Sub
On Error Resume Next
'this turns off the on time event
Application.OnTime dNext, "StartTimerAgain", schedule:=False
End Sub
PLACE THIS CODE IN the specific code module called THISWORKBOOK
Code:
'use this to turn off the Application on time event otherwise workbook will reopen
Sub Auto_Close()
StopTimer
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call StopTimer
If InStr(1, ActiveSheet.Name, ExemptSheets) = 0 Then
Call StartTimerAgain
End If 'not exempt
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call StopTimer
If InStr(1, ActiveSheet.Name, ExemptSheets) > 0 Then
Call StartTimerAgain
End If 'not exempt
End Sub