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!
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?
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 four macros that I want to run every 10 minutes in a specified order. Currently I just set a reminder to run them, but would like to automate the process. Ideally I would love to have a separate macro that repeats all four macros (in order) every 10 mins.
I have tried adding Application.OnTime Now + TimeValue("00:10:00"), "Macro_Name" at the end of the four macros. Issue is two of the macros take some time to process.
Also tried creating a separate Macro "Repeat_10mins", and building it using the OnTime function. Only seems to work once, and doesn't repeat (i.e, runs 10 mins after setting but does not repeat).
Sub Repeat_10mins
Workbooks("Book1.xlsm").RefreshAll
'Repeat Macro1
Application.OnTime Now + TimeValue ("00:10:00"), "Macro1"
'Repeat Macro2
Application.OnTime Now + TimeValue ("00:10:00"), "Macro2"
(etc for Macro 3/4)
End Sub
You will need to create one more macro like this:
Sub Timer()
Application.OnTime Now + TimeValue("00:10:00"), "operation"
End Sub
Where operation is the name of the macro you want to run every 10 minutes.
On the macro you have built you will need to add only one line at the end which is Call Timer.
Explanation:
At the end of the macro operation it will call the macro timer which in 10 minutes will run operation.
Let me know if it works or if you need any more help.
Background: Windows 7, Office 2010
I currently have 2 macros running by buttons, one macro calculates and extracts data and the other macro selects a specific range and sends an email to a specified email.
May I know how I can get this 2 macros to run at a scheduled time when my pc is locked - i.e. (on 'switch user' screen)?
Thank you and I sincerely appreciate any help I can get!
Sub tempo()
tps = Now + TimeValue("00:01:00") 'your refresh rate
Application.OnTime tps, "message_ctrl"
End Sub
Sub message_ctrl()
Call Module1.test 'your macro
Call tempo 'this just relaunch schedule when test() finished
End Sub
Here exemple to refresh every minutes, but you can define day or anything.
You need to start once tempo() or message_ctrl() to start cycle. Maybe at workbook_open.
The key here is the Application.OnTime function.
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.