VBA Stop Application.OnTime Method

2019-08-28 21:26发布

I've encountered a slight issue while using the Application.OnTime method.

As per the below example the code should run every 10 seconds (which it does) however it doesn't stop on button press. After setting a breakpoint, the StopRunning value always shows as false and doesn't change to true when the AutoOffBtn is pressed (the buttons are on a form) I'm using an image for the buttons rather than a command button.

Option Explicit

Private Sub AutoOffBtn_Click()

AutoOffCheck.Value = True
StopRunning = True

End Sub

Private Sub AutoOnBtn_Click()

AutoOnCheck.Value = True
Call AutoOn

End Sub

And the AutoOn code is as follows:

Option Explicit
Public StopRunning As Boolean

Sub AutoOn()

Application.ScreenUpdating = False

Call CCPulseSaveFile
Call CopyHTML
Call SendTest

If Not StopRunning Then Application.OnTime Now + TimeValue("00:00:10"), "AutoOn"

End Sub

Any help to stop the code running would be appreciated, thank you !

标签: excel vba
0条回答
登录 后发表回答