Trigger Code After Inaction

I want my WB to run some code after a period of inactivity (set by me, naturally). I could only find the code to close WB after a period of inactivity, but I want the code to do something else other than closing WB. I found this code to close WB:

This workbook module:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    stop_Countdown
ThisWorkbook.Save
End Sub
Private Sub Workbook_Open()
    start_Countdown
    End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    stop_Countdown
    start_Countdown
    End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    stop_Countdown
    start_Countdown
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
  ByVal Target As Excel.Range)
    stop_Countdown
    start_Countdown
End Sub

Regular module:

Option Explicit
Public Close_Time As Date
Sub start_Countdown()
    Close_Time = Now() + TimeValue("00:00:10")
    Application.OnTime Close_Time, "close_WB"
    End Sub
Sub stop_Countdown()
    Application.OnTime Close_Time, "close_WB", , False
    End Sub
Sub close_wb()
    ThisWorkbook.Close True
    End Sub

In which part of the code should I enter the events that I want WB to do, after inaction, instead of closing WB?

+4
source share
1 answer

You need to make changes to the regular module.

Instead of passing a string close_wb()in a function call, Application.OnTimeyou should specify the name of the procedure that contains everything that you want to execute.

:

Option Explicit
Public Inactivity_Time As Date

Sub start_Countdown()
    Inactivity_Time = Now() + TimeValue("00:00:10")
    Application.OnTime Inactivity_Time, "my_procedure"    ' <- Notice that I changed the name of the procedure here
End Sub

Sub stop_Countdown()
    On Error Resume Next
    Application.OnTime Inactivity_Time, "my_procedure", , False     ' <- And here too.
    On Error GoTo 0
End Sub

Sub my_procedure()
    ' The code to perform you event goes here
End Sub

Application.OnTime . .

: , stop_Countdown() Workbook_BeforeClose: . Workbook_BeforeClose :

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Close_WB
End Sub

:

Public Sub Close_WB()
    stop_CountDown
    ThisWorkbook.Close SaveChanges:=True
End Sub
+2

Source: https://habr.com/ru/post/1625207/


All Articles