Option Explicit
Dim RunAt As Variant
Dim InProgress As Boolean
Sub InitializeMacro()
Dim DefTime As Date
DefTime = TimeSerial(12, 47, 0)
'Stop
If IsEmpty(RunAt) Then
If Time > DefTime Then
If MsgBox("It's already past " & Format(DefTime, "hh:mm:ss") & ". Do you want to run MyMacro after all?", vbQuestion + vbYesNo) = vbYes Then
RunAt = Now
Else
InProgress = False
RunAt = Empty
Exit Sub
End If
Else
RunAt = DefTime
End If
InProgress = True
Application.OnTime RunAt, "MyMacro"
MsgBox "Countdown has started", vbInformation
ElseIf InProgress Then
MsgBox "There is " & Format(RunAt - Time, "hh:mm:ss") & " left to run MyMacro.", vbInformation
End If
End Sub
Sub Auto_Close()
On Error Resume Next
'reset of initiated task
Application.OnTime RunAt, "MyMacro", , False
'If the workbook was closed after MyMacro was executed or
'the countdown procedure has not been initialized,
'then the error handler will ignore the task reset
'cleaning these variables is not necessary,
'cause the file is closed, so the variables will reset themselves.
RunAt = Empty
InProgress = False
End Sub
Sub MyMacro()
InProgress = False
RunAt = Empty
MsgBox "Hello"
End Sub