How to stop a procedure with Application.Ontime

oldfisher

New Member
Joined
Dec 30, 2006
Messages
6
Hi guys,

I've had a procedure with Application.Ontime, following are the codes:

Sub OnTime()
ThisWorkbook.RefreshAll
Application.OnTime earliesttime:=(Now + TimeValue("00:00:05")), procedure:="OnTime"
End Sub

Once the procedure gets started, It seems that it can't be stopped properly.

How can I stop it from running?

Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Check out Help for the OnTime method, it has an argument to unschedule. For that to work, you must store the last scheduled time. Example:
VBA Code:
Option Explicit

Dim nextTime As Double

Sub OnTime()
    ThisWorkbook.RefreshAll
    nextTime = Now + TimeValue("00:00:05")
    Application.OnTime earliesttime:=nextTime, procedure:="OnTime"
End Sub

Sub CancelTimer()
    On Error Resume Next 'In case no items were scheduled
    
    'These must match a previously set OnTime:
    '                                ||||||||              ||||||
    '                                VVVVVVVV              VVVVVV
    Application.OnTime earliesttime:=nextTime, procedure:="OnTime", schedule:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top