Application On Time wont turn off

Flenley

Board Regular
Joined
Jul 31, 2002
Messages
71
Hi

I am using a time method to grab some data every minute and it is working but the problem is I can't turn it off! I have used the example in VB help but to no avail. Here is the code, any suggestions?

Sub Timer_Data()

Application.OnTime Now + TimeValue("00:01:00"), "Timer_Run"

End Sub

Sub End_Timer_Data()

Application.OnTime EarliestTime:=Now + TimeValue("00:01:00"), _
Procedure:="Timer_Run", Schedule:=False

End Sub

Thanks

Angus
 
Hi Flenley,

The reason why it is not cancelling is that the set anc cancel times are NOT the same because the Now value will give the time at the instant the cancellation OnTime is called, not the time the OnTime is scheduled. To get the cancellation to work you must ensure that the two times are the same. Here is one way to do it:

Dim SchTime As Date

Sub Timer_Data()

SchTime = Now + TimeValue("00:01:00")
Application.OnTime SchTime, "Timer_Run"

End Sub

Sub End_Timer_Data()

Application.OnTime EarliestTime:=SchTime, _
Procedure:="Timer_Run", Schedule:=False

End Sub

where SchTime is a variable whose scope encompasses both routines so that both can access it, and it retains its value between calls.
 
Upvote 0

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