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.
Related
I have a UserForm in which I have to run a sub every 5 seconds. I know that I could use Application.Ontime Now + TimeValue("00:00:05"), "my_Procedure", but it seems to work only with macros. When I try it with a sub, I get the message:
Cannot run the macro ''xx\xx\WorkBook1.xlsm'!my_Procedure'. The macro may not be available in this workbook or all macros may be disabled.
I'm an absolute beginner in Excel and VBA. I googled a lot but all answers seem to show how to repeat a macro every x seconds. How can I repeat a sub every x seconds?
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
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.
I need to run an excel macro at 3.00am in the morning. I can set up the task scheduler to be open up excel at 2.55am. However, how do i get excel to execute the macro only after 5 minutes, that it has opened?
You could launch the excel at 3 a.m. and trigger your macro from the WORKBOOK_OPEN event by adding a
Private Sub Workbook_Open()
RunUrMacro()
End Sub
To your workbook.
If you absolutely need to load at 2.55 and run at 3 then you could sleep the application for 5 minutes using Application.Wait
As Nishant pointed out, you should use the Workbook_Open procedure to trigger a macro when the workbook opens.
The best way is to open you workbook at 3:00am and Workbook_Open will trigger the macro.
Yet, if you really need to wait, you'd better use Application OnTime like this:
Sub RunOnTime()
dTime = Now + TimeSerial(0, 0, 10)
Application.OnTime dTime, "RunMe"
End Sub