I looked through every topic I've found in here, but couldn't figure what is wrong with the below.
I am using Application.OnTime to run a Module every minutes. The problem is, when I close the worksheet, Excel reopens the sheet the next time the Macro should run.
I have attempted to use the following fix, to no avail.
Option Explicit
Dim dTime As Date
Public Function AutoRun2()
dTime = Now + TimeValue("00:01:00")
Application.OnTime dTime, "AutoClear"
End Function
Sub StopAutoRun2()
Application.OnTime dTime, "AutoRun2", , False
End Sub
and I have the following in ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopAutoRun
Call StopAutoRun2
End Sub
What am I doing wrong here?
Change "AutoRun2" to "AutoClear" in your stop sub as this is the actual macro that you want to stop being scheduled with the False argument.
Related
I have this public sub defined in Module 1 of my excel workbook
Public Sub AutoUpdate()
Application.OnTime TimeValue("22:40:00"), "GetTime"
End Sub
And in my workbook code i have this to call this AutoUpdate method.
Public Sub Workbook_Open()
Call AutoUpdate
End Sub
My problem is that when i open the file it ways the time to be 22:40:00 and it calls "GetTime" but then on the next day the excel either crashes or the function is not called again.
Is this Application.OnTime method working for each day without needing to reopen ( refresh ) the workbook again.
Try using the following code:
Private Sub Workbook_Open()
Call DailyRun
End Sub
DailyRun code:
Sub DailyRun()
Application.OnTime TimeValue("22:40:00"), "GetTime"
Application.OnTime TimeValue("23:59:00"), "DailyRun"
End Sub
The DailyRun macro calls itself at the end of each day to create new Ontime event.
If you want to run the macro using Windows Task Scheduler, add your macro to Workbook Open event:
Private Sub Workbook_Open()
Application.OnTime TimeValue("22:40:00"), "GetTime"
Me.Close
End Sub
Don't forget to close your workbook.
I want to start and stop two timers in my excel worksheet. This is what i have:
When worksheet is opened, this module is triggered:
Sub SetOpenTimer()
OpenTimer = Now + TimeValue("00:00:10")
Application.OnTime OpenTimer, "StartTimerShutdownForm"
End Sub
After 10 seconds (in this example), the sub startimershutdownform is triggered:
Sub StartTimerShutdownForm()
UserForm1.Show
End Sub
This displays a userform where a message is displayed. In the initialize sub in the Userform a sub setformtimer is triggered:
Sub setFormTimer()
FormTimer = Now + TimeValue("00:00:10")
Application.OnTime FormTimer, "Shutdown"
End Sub
If this timer in setformtimer ends, the sub shutdown is triggerd
this sub closes the workbook, but in the Userform there is a button for the user to cancel the setformtimer:
Sub stopFormTimer()
Application.OnTime EarliestTime:=FormTimer, _
Procedure:="setOpenTimer", Schedule:=False
End Sub
This suppose to stop the setformtimer, but it does not work.
When this timer is stopped, it suppose to restart the timer that is triggered when the document is being opened, so it all restarts, but the stop timer code does not work.
In the module where stopFormTimer, setFormTimer and StartTimerShutdownForm is located, at the top I declare these
Dim OpenTimer As Date
Dim FormTimer As Date
What am i missing here? Why cant i stop the timer and restart the first timer?
I think the problem is you have different proc names in the two subs. They must match. I'd try:
Sub setFormTimer()
FormTimer = Now + TimeValue("00:00:10")
Application.OnTime EarliestTime:=FormTimer, Procedure:="Shutdown", Schedule:=True
End Sub
Sub stopFormTimer()
Application.OnTime EarliestTime:=FormTimer, Procedure:="Shutdown", Schedule:=False
End Sub
So I've built this timer macro that more than one youtube person has specified how to build in this exact same way, but it still refuses to work.
Sub StartTimer()
Application.OnTime Now + TimeValue("00:00:01"), "nextTime"
End Sub
Sub nextTime()
If Sheet1.Range("I1") = 0 Then Exit Sub
End If
Range("I1").Value = Range("I1").Value - TimeValue("00:00:01")
StartTimer
End Sub
Sub StopTimer()
Application.OnTime Now + TimeValue("00:00:01"), "nextTime", , False
End Sub
The problem arises in the line of code within the StartTimer Subroutine. The nextTime method simply does not get recognized, as the Error says:
The macro may not be available in this workbook or all macros may be disabled. This is obviously not the case as all of my other macros work, and the subroutine is right there. What are your suggestions?
Move your code out of the worksheet code module and put it into a normal code module.
Alternatively, you could call it by fully qualifying it as Sheet1.nextTime (where Sheet1 needs to be the worksheet code name where the code resides), but I think it is probably better to put it into the project level code module.
You also need to remove the End If from the nextTime subroutine.
And you need to fix the StopTimer routine to pass the correct time - i.e. the time at which the next event is due to run.
Public nextRunTime As Date
Sub StartTimer()
nextRunTime = Now + TimeValue("00:00:01")
Application.OnTime nextRunTime, "nextTime"
End Sub
Sub nextTime()
If Sheet1.Range("I1") = 0 Then Exit Sub
Range("I1").Value = Range("I1").Value - TimeValue("00:00:01")
StartTimer
End Sub
Sub StopTimer()
Application.OnTime nextRunTime, "nextTime", , False
End Sub
I got some problem with application.ontime maybe somebody knows the solution
From the beggining
Private Sub Workbook_Open()
'some code
call macro_name
end sub
sub macro_name
'some code refreshing data on form
call start_timer
end sub
dim start_time as date
Sub start_timer()
start_time = Now() + TimeValue("00:00:05")
Application.OnTime start_time, "macro_name"
End Sub
Sub end_timer()
Application.OnTime start_time, "macro_name", , False
'MsgBox "done" i put this to be sure that code is executed
End Sub
Private Sub Workbook_BeforeClose(cancel As Boolean)
'some code
call end_timer
end sub
problem is that the timer doesn't stop. If i have some other workbook open and i close the one with ontime code it starts again at scheduled time
if i put buttons on a form
Private Sub CommandButton1_Click()
start_timer
End Sub
Private Sub CommandButton2_Click()
end_timer
End Sub
everything works ok and ontime is canceled.
I solved it partly by putting the
call end_timer
on the form closing button
Private Sub Image1_Click()
end_timer
ThisWorkbook.Close
End Sub
but if workbook will be closed some other way than button ontime won't be canceled
sorry for my english i'll try to explain better if something is unclear
I have a need to run a piece of code every 120 seconds. I am looking for an easy way to do this in VBA. I know that it would be possible to get the timer value from the Auto_Open event to prevent having to use a magic number, but I can't quite get how to fire off a timer to get something to run every 120 seconds.
I don't really want to use an infinite loop with a sleep if I can avoid it.
EDIT:
Cross-post based on an answer provided is at: Excel VBA Application.OnTime. I think its a bad idea to use this... thoughts either way?
When the workbook first opens, execute this code:
alertTime = Now + TimeValue("00:02:00")
Application.OnTime alertTime, "EventMacro"
Then just have a macro in the workbook called "EventMacro" that will repeat it.
Public Sub EventMacro()
'... Execute your actions here'
alertTime = Now + TimeValue("00:02:00")
Application.OnTime alertTime, "EventMacro"
End Sub
Yes, you can use Application.OnTime for this and then put it in a loop. It's sort of like an alarm clock where you keep hittig the snooze button for when you want it to ring again. The following updates Cell A1 every three seconds with the time.
Dim TimerActive As Boolean
Sub StartTimer()
Start_Timer
End Sub
Private Sub Start_Timer()
TimerActive = True
Application.OnTime Now() + TimeValue("00:00:03"), "Timer"
End Sub
Private Sub Stop_Timer()
TimerActive = False
End Sub
Private Sub Timer()
If TimerActive Then
ActiveSheet.Cells(1, 1).Value = Time
Application.OnTime Now() + TimeValue("00:00:03"), "Timer"
End If
End Sub
You can put the StartTimer procedure in your Auto_Open event and change what is done in the Timer proceedure (right now it is just updating the time in A1 with ActiveSheet.Cells(1, 1).Value = Time).
Note: you'll want the code (besides StartTimer) in a module, not a worksheet module. If you have it in a worksheet module, the code requires slight modification.
In Workbook events:
Private Sub Workbook_Open()
RunEveryTwoMinutes
End Sub
In a module:
Sub RunEveryTwoMinutes()
//Add code here for whatever you want to happen
Application.OnTime Now + TimeValue("00:02:00"), "RunEveryTwoMinutes"
End Sub
If you only want the first piece of code to execute after the workbook opens then just add a delay of 2 minutes into the Workbook_Open event
(This is paraphrased from the MS Access help files. I'm sure XL has something similar.) Basically, TimerInterval is a form-level property. Once set, use the sub Form_Timer to carry out your intended action.
Sub Form_Load()
Me.TimerInterval = 1000 '1000 = 1 second
End Sub
Sub Form_Timer()
'Do Stuff
End Sub
I've found that using OnTime can be painful, particularly when:
You're trying to code and the focus on the window gets interrupted
every time the event triggers.
You have multiple workbooks open, you close the one that's supposed to use the timer, and it keeps triggering and reopening the workbook (if you forgot to kill the event properly).
This article by Chip Pearson was very illuminating. I prefer to use the Windows Timer now, instead of OnTime.
My solution:
Option Explicit
Public datHora As Date
Function Cronometro(action As Integer) As Integer
'This return the seconds between two >calls
Cronometro = 0
If action = 1 Then 'Start
datHora = Now
End If
If action = 2 Then 'Time until that moment
Cronometro = DateDiff("s", datHora, Now)
End If
End Function
How to use? Easy...
dummy= Cronometro(1) ' This starts the timer
seconds= Cronometro(2) ' This returns the seconds between the first call and this one