Close Workbook when Sheet is inactive

eli_m

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

I currently have this regular Module:
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()
        MsgBox "Timed out after 30 minutes - Your work has been saved and closed"
    With ThisWorkbook
        Application.DisplayAlerts = False
        .Close Savechanges:=True
        Application.DisplayAlerts = True
    End With
End Sub


And I have these in My Sheet:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Call StopTimer 'Stop Timeout timer
    Call SetTimer 'Set Timeout timer

If Application.CutCopyMode = False Then
Application.Calculate ' Refresh for Grey Line
End If
End Sub


VBA Code:
Private Sub Worksheet_Calculate()

    Call StopTimer 'Stop Timeout timer
    Call SetTimer 'Set Timeout timer

' Ignore Errors after Sorting
Dim r As Range: Set r = Range("A2:AW200")
Dim cel As Range

For Each cel In r
  With cel
    .Errors(8).Ignore = True 'Data Validation Error
    .Errors(9).Ignore = True 'Inconsistent Error
    .Errors(6).Ignore = True 'Lock Error
  End With
Next cel
End Sub


All is working fine and the Workbook closes after 30 minutes.
I want to make it when I am on another sheet that it doesn't close.

I tried stopping the time with the below code but with no success:
VBA Code:
Private Sub Worksheet_Activate()
   Call StopTimer 'Stop Timeout timer
  ThisWorkbook.RefreshAll ' Auto Refresh Pivot Tables
 
On Error Resume Next

End Sub

Any help would be great!

Thanks in advance
 
Anyone able to help with this :)?

Thanks in advance,
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
It looks like if you try to stop the timer before it is running it will error out.

Try changing the stoptimer to this. This way, if the timer is already stopped for some reason it will ignore the error and continue.

VBA Code:
Sub StopTimer()
    On Error Resume Next
    Application.OnTime DownTime, "ShutDown", , False
 End Sub
 
Upvote 0
Solution
It looks like if you try to stop the timer before it is running it will error out.

Try changing the stoptimer to this. This way, if the timer is already stopped for some reason it will ignore the error and continue.

VBA Code:
Sub StopTimer()
    On Error Resume Next
    Application.OnTime DownTime, "ShutDown", , False
 End Sub
amazing! thank you - this is what worked
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
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