


There are times this is useful, like when you cannot determine programmatically when you will need the macro, but you will know before the end of the day and it must be run overnight. However, if you need to run the macro with the scheduler to schedule it every day, you just shifted the burden from running the macro directly to running the scheduler. If you run the scheduler once, you will get a single scheduled event. Make sure you set the workbook to be trusted before using this method or you won’t get much automation benefit. If security settings are such that the opened workbook is not trusted, the macro cannot run until someone clicks “Enable Macros”. Important: the workbook will open and try to execute the macro. OnTime Now + TimeSerial ( 0, 0, 3 ), "C:\Work Files\Other Workbook.xlsm!Module1.main" A call to a workbook named Other Workbook.xlsm with a macro named main in it will look like this:Īpplication. Keep in mind that Application.OnTime lives at the application level and schedules there, so you can run the OnTime method from one workbook and it will execute macros in another workbook as long as you properly specify the name of the other workbook. I have plenty of workbooks that have subroutines named main, for example. It’s certainly allowed, but I don’t recommend naming your macros exactly the same in different modules because it can become confusing for a human.īelieve it or not, you can extend the Application.OnTime method to other workbooks, even if they have identically-named macros. If the Application.OnTime line runs in Module1 and there is a macro entitled macro_to_schedule there, we must specify we want the identically-named macro from Module2. In this snippet, we are running the macro_to_schedule from Module2.
#Vba application ontime code#
You would execute a code snippet like this to make sure your macro runs before you even arrive:Īpplication. Let’s say you have to run a macro every day at 7am before you come into the office. If a task must be run every day at the same time, it makes sense to apply the specific time technique. Conversely, if there is something preventing the called macro from running, LatestTime is essentially the time at which Excel stops trying to run the macro. As long as nothing prevents the called macro from running, this is synonymous with the start time. You can also make other scheduling decisions, like scheduling a macro to run relative to a future date based on some user input.Īgain, the argument EarliestTime is your start time, and the scheduling mechanism will try to execute the specified macro at this time.

The most common ways are via specific times (1am, 11:45, 19:30) or a time relative to the existing time. There are many ways to schedule your macros to run using the OnTime method.

Fortunately, Excel makes scheduled macro automation simple with the Application.OnTime method. Pushing buttons just to make a code run is particularly annoying, especially when the code is supposed to run at the same time every time. Programmers love to make things more efficient.
