I would like to automatize scheduled report on my company. I found out a method called Application OnTime in Excel, which after some reading looks like a perfect match.
The task is following, on chosen periods during day I would like to import data through powerquery and then save a file. I would like to work it all the time. My simple code looks like that:
Private Sub Workbook_Open()
Application.OnTime TimeValue("06:30:00"), "Module1.MyMacro"
Application.OnTime TimeValue("09:30:00"), "Module1.MyMacro"
Application.OnTime TimeValue("12:00:00"), "Module1.MyMacro"
Application.OnTime TimeValue("14:29:00"), "Module1.MyMacro"
End Sub
Sub MyMacro()
ActiveWorkbook.RefreshAll
Application.Wait (Now + TimeValue("0:05:00"))
ActiveWorkbook.Save
End Sub
Due to overlong refresh of the data I gave extra 5 mins before saving file. However I encountered problem, after some time my script stops working. I have tried different solutions from the Internet, unfortunately I couldn't complete the task on my own.
I have a request If someone could tell me abaout what I've forgotten and explain the issue.
The first parameter is EarliestTime. Your post lack some detail, but I guess it'll start a bit late. The third parameter is LatestTime. Try something like this:
Application.OnTime TimeValue("06:30:00"), "Module1.MyMacro", TimeValue("06:40:00")
https://learn.microsoft.com/en-us/office/vba/api/excel.application.ontime
Related
I have the following VBA subroutine which is calls itself every second:
Public Sub AllTheTime()
iTimerSet = Now + TimeValue("00:00:01")
Call runMyFunction
Application.OnTime iTimerSet, "AllTheTime"
End Sub
This script is running perfectly: It is recalling itself every second and calculating some stuff.
But when I install the Bloomberg Office Add-On (Bloomberg Ribbon) it is no longer working: It is running exactly one time then it is stopped. I checked this with Debug.Print. It seems that Bloomberg is stopping the "Application.OnTime" method.
Do you have any ideas what causes this behaviour?
My macro is to update text imported from a website every second.
It produces this error:
Cannot run the macro "'/Users/(name)/Desktop/(foldername)/Book1.xlsx'!List1.UpdateCell". The macro may not be available in this workbook or all macros may be disabled.
Sub UpdateCell()
ActiveWorkbook.RefreshAll
Application.OnTime Now + TimeValue("00:00:1"), "List1.UpdateCell"
End Sub
In your Application.OnTime, the second parameter that holds procedure stored as a string, throws that error, "List1.UpdateCell" revise and correct it.
I changed the "List1.UpdateCell" to just "UpdateCell".
I have a rather odd problem. I am using Application.Ontime TimeValue() to run excel macro at given time (refresh some data and send an email). It used to work great until 3 weeks ago but since, the macro are executed at "random time", i.e with some delay from 1 to 30 minutes.
I tried on 2 computers with up to date windows and Excel 365 versions. Issues are the same. All macro run well when I execute them manually or without the Application.Ontime TimeValue().
Application.OnTime TimeValue("16:00:00"), "Update"
Application.OnTime TimeValue("16:03:00"), "SendPDF"
Application.OnTime TimeValue("16:04:00"), "SaveThisWorkbook"
As said before, I have no error message and the Application. OnTime add some random delay to the execution.
Thank you for your help!
I am attempting to use a self-activating OnTime sub to edit some text in a UF control. However, I find that an Application.OnTime event won't start working until the UserForm is closed.
As per this SO thread, I have placed the self-activating sub in a public sub in a regular module. However, this is to no avail.
UF sub
Private Sub UserForm_Initialize
TEST.loadingdots
End Sub
Regular sub
Public Sub loadingdots
Debug.Print 4
Application.OnTime Now + TimeValue("00:00:02"), "loadingdots"
End Sub
The first "4" gets printed, then nothing. When I close the UF, the procedure gets executed as expected.
How can I work around this?
Answering my own question for future reference
When a UF holds a RefEdit control, an OnTime event won't be executed.
I created a new, blank UF, which caused no issues whatsoever.
Seems like RefEdits need to be approached with caution, similarly to how using a RefEdit control in a modeless userform forces the user to close the Excel application through task manager.
I am trying to run a certain macro in very short intervals, such as every second, from a certain point in time until a certain end point in time. This means I need a starting point and a cutoff point. I cannot use the Workbook_Open() Event, since I already have other macros triggering at different times after the opening of the Workbook.
The basic line I use to trigger the Macro once a second is this psuedocode:
Application.OnTime Now + TimeValue("00:00:01"), "Path to Macro"
From my experiments so far, any attempt I made ended up with two results. In the first case, it ran from the moment I opened the Workbook and with the appropriate schedule of once per second. However, the first case was suboptimal, as I needed it to wait a bit before it started up. In the second case, it ran at the time I wanted it to start - but it ran only once, which was also not what I wanted to happen.
To summarize:
I need something like the code line to start running 15 minutes after the Workbook is opened and stop 3 hours later.
What other timed macros are started from workbook_open, and why are these interfering? It sounds like you're limiting yourself unnecessarily. Here's how to address the issue:
Workbook_open should use application.ontime to call a general function do_timed_events. The do_timed_events function should re-add itself using application.ontime each time it is run. It should also keep track of state. For its first few runs, it should perform the other specific tasks, then wait 15m, then start performing the every-second task.
Here's some pseudocode:
private var do_timed_events_state as string
sub do_timed_events
if do_timed_events_state = "" then
do_task_1(arg1,arg2)
do_timed_events_state = "task_2"
Application.OnTime Now + TimeValue("00:00:01"), "do_timed_events"
elseif do_timed_events_state = "task_2" then
do_timed_events_state = "repeating_task"
Application.OnTime Now + TimeValue("00:00:01"), "do_timed_events"
elseif do_timed_events_state = "repeating_task" then
Application.OnTime Now + TimeValue("00:00:01"), "do_timed_events"
end if
end sub
You can probably come up with a better design than me on this one.