- Schedules a procedure to be run at a specified time
- Clear Schedules
- Use Windows Task Scheduler to open a workbook at a specified time
Application.OnTime
Application.OnTime Time Procedure Schedule
The Application.OnTime
method can make macros run automatically, once you've done some setup. This method required at-least two arguments:
- Time:
The time when you want this procedure to be run. - Procedure:
The name of the Sub-procedure to execute when the time event occurs. - Schedule: (Optional)
Set it toFalse
to clear a previously set procedure (see, how to cancelOnTime
schedule.
Suppose you have a macro "MyMacro
" that you want to run daily at 13:00 (1:00 P.M.). First you need to determine how to use the OnTime
method. You can do this using the Workbook_Open
event in the private module of the Workbook object.
Press ALT+F11
to open the VBE (Visual Basic Editor) and then open the ThisWorkbook
module for the Workbook object from the Project window. Enter the following code:
Private Sub Workbook_Open( ) Application.OnTime TimeValue("13:00:00"), "MyMacro" End Sub
MyMacro
should be the name of the macro you want to run. It should reside in a standard module and contain the OnTime
method, as follows:
Sub MyMacro( ) Application.OnTime TimeValue("13:00:00"), "MyMacro" 'YOUR CODE End Sub
The previous code snippets will run the procedure MyMacro
at 13:00 each day, so long as Excel is open.
Clear a Schedule
Now suppose you want to run MyMacro
at 15-minute intervals after opening your workbook.
Again you will kick it off as soon as the workbook opens, so press ALT+F11
to open the VBE and then open the module for the Workbook object from the Project window and enter the following code:
Private Sub Workbook_BeforeClose(Cancel As Boolean) Application.OnTime dTime, "MyMacro", , False End Sub Private Sub Workbook_Open( ) Application.OnTime Now + TimeValue("00:15:00"), "MyMacro" End Sub
In any standard module (accessed by selecting Insert » Module
in VBE), enter the following code:
Public dTime As Date Sub MyMacro( ) dTime = Now + TimeValue("00:15:00") Application.OnTime dTime, "MyMacro" 'YOUR CODE End Sub
Note how you pass the time of 15 minutes to the public variable dTime
. This is so that you can have the OnTime
method cancelled in the Workbook_BeforeClose
event by setting the optional Schedule
argument to False.
The Schedule
argument is True
by default, so by setting it to False
, you are telling Excel to cancel the OnTime
method that is set to run at a specified time.
If you didn't pass the time to a variable, Excel would not know which OnTime
method to cancel, as Now + TimeValue("00:15:00")
is not static, but becomes static when passed to a variable. If you didn't set the optional Schedule
argument to False, the workbook would open automatically every 15 minutes after you close it and run MyMacro
.
Use Windows Task Scheduler to Schedule Open a Workbook
If you want to run a macro on a schedule, you need to keep Excel open. Otherwise, the macro will not execute at the specified time. A workaround is to use Windows Task Scheduler to launch the Excel workbook that contains the macro. This way, the macro will run automatically when the workbook opens.
Here's how to do it:
- Open Windows Task Scheduler:
PressWin + S
, type "Task Scheduler" and press Enter to open Task Scheduler. - Create a Basic Task:
In Task Scheduler, click onCreate Basic Task...
from the right-hand sidebar.
- Follow the wizard to name and describe your task.
- Choose a Trigger:
Select "Daily" or "Weekly," depending on your preferred schedule.
- Set the start date and time to when you want the macro to run.
- Choose Action:
Select "Start a Program" as the action.
- Browse for the Excel workbook:
In the "Program/script" field, browse for the Excel workbook. For example:C:\Path\To\Your\File.xlsm
.
- Complete the wizard, review your settings, and click
Finish
.
You can right-click on your task in Task Scheduler and select "Run" to test it immediately.
Now, Windows Task Scheduler will open Excel workbook and run your macro at the specified time.