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
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 want to evaluate how long the spreadsheet has been open and once it hits a certain time (say an hour), warn the user that it will automatically close in 15 mins.
I have a tab called Reference where I can put auto-open code to log when they opened it but I want the spreadsheet to check unprompted at certain intervals how long the sheet has been open.
I want to force a close where a user has accidentally left a spreadsheet open so it's important it isn't assessed based on user input.
Note - I don't care if they lose work, I just want to close the spreadsheet.
To schedule the call of a routine, use Application.OnTime. To ensure that the routine is scheduled to "one hour after opening", create a Workbook-Open sub (in ThisWorkbook-Module) and put something like this into it:
Private Sub Workbook_Open()
Application.OnTime DateAdd("h", 1, Now), "InformUser"
End Sub
This will call the Sub InformUser one hour after the Workbook was opened. In this routine you could add another call to OnTime. However, you need to be a little bit careful how to inform the user. If you simple use MsgBox but the user is not reacting to that, the MsgBox-Window stays active and as it is a modal window, the code will not continue to run and the OnTime-procedure will never be triggered. It's not easy to create a MsgBox with a TimeOut (see Display a message box with a timeout value), so maybe create a simple form and show it Non-Modal.
Sub InformUser()
InfoForm.Show modal:=False
Application.OnTime DateAdd("n", 15, Now), "ShutDown" ' "n": Minutes ("m" stands for months)
End Sub
Sub ShutDown
ThisWorkbook.Close SaveChanges:=True ' Or "False" if you don't want to save - use on your own risk...
End Sub
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 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 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.