Save and close after inactivity Causing crashing

PaulOPTC

New Member
Joined
Jan 13, 2022
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Good Afternoon,

I am having an issues with a particular code:


In the ThisWorkbook module:

Option Explicit
Private Sub Workbook_Open()
LastActivityTime = Now()
Check_Inactivity
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
LastActivityTime = Now()
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
LastActivityTime = Now()
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
LastActivityTime = Now()
End Sub

And, in a standard moduleOption Explicit
Public LastActivityTime As Date
Sub Check_Inactivity()
Const Inactivity_Delay As Date = #12:05:00 AM#
If LastActivityTime + Inactivity_Delay < Now() Then
ThisWorkbook.Close True
Else
Application.OnTime LastActivityTime + Inactivity_Delay, "Check_Inactivity"
End If
End Sub



The code works perfectly to what I would like it to do, but often times it completely breaks my ability to open the spreadsheet at all.
It will get to 100%, then crash, or just not even show the excel window at all. It causes other sheets to crash.


Does anyone else have a better code that runs more efficiently?

Ideally it would save and close after 5 mins of inactivity.

Thank you!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this:

In the ThisWorkbook module:
VBA Code:
Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  On Error Resume Next
  Application.OnTime Now, "Check_Inactivity", , False
End Sub

Private Sub Workbook_Open()
  LastActivityTime = Now()
  Check_Inactivity
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  LastActivityTime = Now()
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  LastActivityTime = Now()
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  LastActivityTime = Now()
End Sub

in a standard module:
VBA Code:
Public LastActivityTime As Date
Public xNow

Sub Check_Inactivity()
  Dim Inactivity_Delay
                          'hours:minutes:seconds
  Inactivity_Delay = TimeValue("00:00:15")
  
  If LastActivityTime + Inactivity_Delay < Now() Then
    On Error Resume Next
    Application.OnTime xNow + Inactivity_Delay, "Check_Inactivity", , False
    ThisWorkbook.Close True
  Else
    xNow = Now
    Application.OnTime xNow + Inactivity_Delay, "Check_Inactivity"
  End If
End Sub
 
Upvote 0
Solution
Try this:

In the ThisWorkbook module:
VBA Code:
Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  On Error Resume Next
  Application.OnTime Now, "Check_Inactivity", , False
End Sub

Private Sub Workbook_Open()
  LastActivityTime = Now()
  Check_Inactivity
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
  LastActivityTime = Now()
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  LastActivityTime = Now()
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  LastActivityTime = Now()
End Sub

in a standard module:
VBA Code:
Public LastActivityTime As Date
Public xNow

Sub Check_Inactivity()
  Dim Inactivity_Delay
                          'hours:minutes:seconds
  Inactivity_Delay = TimeValue("00:00:15")
 
  If LastActivityTime + Inactivity_Delay < Now() Then
    On Error Resume Next
    Application.OnTime xNow + Inactivity_Delay, "Check_Inactivity", , False
    ThisWorkbook.Close True
  Else
    xNow = Now
    Application.OnTime xNow + Inactivity_Delay, "Check_Inactivity"
  End If
End Sub
Awesome, Seems to be working!

I just set the time to 5 mins instead of 15 seconds (My guys are not the quickest spreadsheet users)

Ill let you know if I have any issues with it breaking my sheet.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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