
I have tried different solutions from the Internet, unfortunately I couldn't complete the task on my own. However I encountered problem, after some time my script stops working.
EXCEL APPLICATION ONTIME CODE
My simple code looks like that: Private Sub Workbook_Open()Īpplication.OnTime TimeValue("06:30:00"), "Module1.MyMacro"Īpplication.OnTime TimeValue("09:30:00"), "Module1.MyMacro"Īpplication.OnTime TimeValue("12:00:00"), "Module1.MyMacro"Īpplication.OnTime TimeValue("14:29:00"), "Module1.MyMacro"Īpplication.Wait (Now + TimeValue("0:05:00"))ĭue to overlong refresh of the data I gave extra 5 mins before saving file. The task is following, on chosen periods during day I would like to import data through powerquery and then save a file. I found out a method called Application OnTime in Excel, which after some reading looks like a perfect match. The Shift+F10 key event, on the other hand, applies to all open workbooks.I would like to automatize scheduled report on my company. Remember that the Worksheet_BeforeRightClick procedure is valid only in its own workbook. Sub Setup_OnKey() Application.OnKey "" End Sub Sub NoShiftF10() MsgBox "Nice try, but that doesn't work either." End SubĪfter the SetupNoShiftF10 procedure is executed, pressing Shift+F10 displays the message box shown in Figure 19-11. The net effect is that pressing PgDn moves the cursor down one row, and pressing PgUp moves the cursor up one row. After the Setup_OnKey procedure is executed, pressing PgDn executes the PgDn_Sub procedure, and pressing PgUp executes the PgUp_Sub procedure.

This event reassigns the PgDn and PgUp keys. The following example uses the OnKey method to set up an OnKey event. To see an example of a repeating OnTime event, see the analog clock example in Chapter 18. To prevent this, use a Workbook_BeforeClose event procedure that contains the following statement: Call StopClock In other words, if you close the workbook without running the StopClock procedure, the workbook will reopen itself in five seconds ( assuming that Excel is still running). The OnTime event persists even after the workbook is closed. This example, named ontime event demo.xlsm, is available on the companion CD-ROM.ĭim NextTick As Date Sub UpdateClock() ' Updates cell A1 with the current time ThisWorkbook.Sheets(1).Range("A1") = Time ' Set up the next event five seconds from now NextTick = Now + TimeValue("00:00:05") Application.OnTime NextTick, "UpdateClock" End Sub Sub StopClock() ' Cancels the OnTime event (stops the clock) On Error Resume Next Application.OnTime NextTick, "UpdateClock",, False End Sub Caution Note that NextTick is a module-level variable that stores the time for the next event. To stop the events, execute the StopClock procedure (which cancels the event). This event re-runs the UpdateClock procedure.

Executing the UpdateClock procedures writes the time to cell A1 and also programs another event five seconds later. In this case, cell A1 is updated with the current time every five seconds.

EXCEL APPLICATION ONTIME HOW TO
The two procedures that follow demonstrate how to program a repeated event. If you plan to use this method, you should refer to the online help for complete details. The OnTime method has two additional arguments. The following statement runs the DisplayAlarm procedure at 12:01 a.m.

You can also use the OnTime method to schedule a procedure on a particular day. Application.OnTime Now + TimeValue("00:20:00"), "DisplayAlarm"
