Excel 2010 Macro Schedule Macro to Run Periodically

 

Some users like to ask Excel to run a certain macro every 2 minutes. However, if you accidentally left the workbook in Edit mode while going to lunch or to a meeting and a macro gets delayed, you don’t want dozens of updates running in a matter of seconds. The easy solution is to have the ScheduleAnything procedure repeatedly schedule itself to run again in 2 minutes.

 

To schedule a run in 2 minutes and then perform CaptureData:

Sub ScheduleAnything()

‘ This macro can be used to schedule anything

‘ Enter how often you want to run the macro in hours and minutes

    WaitHours = 0

    WaitMin = 2

    WaitSec = 0

    NameOfThisProcedure = “ScheduleAnything”

    NameOfScheduledProc = “CaptureData”

‘ --- End of Input Section -------

‘ Determine the next time this should run

    NextTime = Time + TimeSerial(WaitHours, WaitMin, WaitSec)

‘ Schedule ThisProcedure to run then

    Application.OnTime EarliestTime:=NextTime, Procedure:=NameOfThisProcedure

‘ Get the Data

    Application.Run NameOfScheduledProc

End Sub