Schedule a VBA Macro (ALARM Setting)

Heylo, How is your office life going on? I pray for all of you and wish you lots of wealth and a very good health. Coming to our topic, we're actually gonna discuss Application.OnTime Object of Application class.
For details: Please visit Microsoft MSDN here:
http://msdn.microsoft.com/en-us/library/aa195809(office.11).aspx

We used Now + TimeValue(time) in our example to schedule something to be run when a specific amount of time (counting from now) has elapsed as TimeValue(time) is used to schedule something to be run a specific time.

For e.g.
1. This example runs my_Procedure 15 seconds from now.
Application.OnTime Now + TimeValue("00:00:15"), "my_Procedure"

2. This example runs my_Procedure at 5 P.M.
Application.OnTime TimeValue("17:00:00"), "my_Procedure"


Sample Code:
Option Explicit


Sub ScheduleAMacro()
Application.OnTime Now + TimeSerial(0, 1, 0), "AlarmMacro"
End Sub

'This should be the macro that runs after one minute
'We simply pops up a message box.
Sub AlarmMacro()
MsgBox "The code was just executed!"
End Sub



Anonymous said...

Hi..Do we need to left excel open till to run or schdule a macro to run or it can run by itself...

Anonymous said...

excel must be running.

VBA code is compiled during excel work