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?
Related
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
I would like a VBA Excel macro to run every morning # 8:30AM and then every 2 hours thereafter with the final one # 4:30pm (10:30am, 12:30pm, 2:30pm,4:30pm).
I have the following two scripts so far, however cannot seem to figure out how to have it start # 8:30AM:
Private Sub Workbook_Open()
Call SetTimeToRun
End Sub
Private Sub SetTimeToRun()
Application.OnTime Now() + TimeValue("02:00:00"), "LiveBook"
End Sub
If you want to run it a specific amount eg 3 times then simply do:
Application.OnTime Now() + TimeValue("02:00:00"), "LiveBook"
Application.OnTime Now() + TimeValue("04:00:00"), "LiveBook"
Application.OnTime Now() + TimeValue("06:00:00"), "LiveBook"
If you want to run it continously every 2 hours without limitation then you need to add
Call SetTimeToRun
to your procedure LiveBook. For Example
Public Sub LiveBook()
Call SetTimeToRun
'your code here …
End Sub
So everytime LiveBook runs it initiates the next run in 2 hours.
But note that the workbook needs to be open all the time in that machine otherwise the timed procedures will not run.
If you need to run a macro at a specific times like 8:30 am, 10:30 am. I recommend to use the windows scheduler to run a VB script or something that opens the workbook, runs the macro and closes the workbook. But note that no one else can use the workbook or the scheduler will fail to open the workbook (only one person can open a workbook at a time for editing).
Also see How to set recurring schedule for xlsm file using Windows Task Scheduler.
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 try to run the following code on specific time, the code ran very first time, after that when I change time it's not working, code is saved in ThisWorkbook.
Any Suggestions?
Private Sub workbook_open()
'Run AlchemyTest_2 at 1:50 pm
Application.OnTime ("1:50:00"), "AlchemyTest_2"
End Sub
Since your code is contained in the workbook_open function, it will only run when you first open the workbook.
If you want to run that line of code, you can paste it into the immediate window (Ctrl+G if it isn't visible) and hitting enter.
The immediate window serves 2 purposes. First, it provides a place for code to be entered that you want to be run immediately, and the output of that code, if it returns anything. Second, it is the output location for Debug.Print.
You mention changing the time, which suggests that you are trying to to get AlchemyTest_2 to run (almost) immediately. If that is the case, you can either paste AlchemyTest_2 into the immediate window and hit enter, or place your cursor in the function and run it (F5).
The time supplied is not a delta time, it's absolute. Try it like this instead:
Private Sub workbook_open()
'Run AlchemyTest_2 at 1:50 pm
Application.OnTime Cdate(Clng(Now)) + TimeValue("13:50:00"), "AlchemyTest_2"
End Sub
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.