Option Explicit
Dim RunAt As Variant
Dim InProgress As Boolean
Sub InitializeMacro()
Dim DefTime As Date
DefTime = TimeSerial(15, 8, 0)
If IsEmpty(RunAt) Then
If Time > DefTime Then
RunAt = Date + 1 + DefTime
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_Open()
Call InitializeMacro
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"
'if you want to automatically define a new task for the next day
'then uncomment the bottom line:
Application.OnTime Now, "InitializeMacro"
End Sub