我已经阅读了一些关于如何停止 VBA
我使用它每隔x秒拉取一些数据。我知道当我调用
我还尝试引入多个命令(例如尝试引起错误)来停止执行,但仍然不起作用!程序仍然在运行...这是我的代码:
在工作表代码中:
我知道我可能增加了几个额外的措施来阻止执行,但似乎都没有奏效!
Application.OnTime
过程的问题/线程(例如 this 和 this),但我就是无法让它停止!我使用它每隔x秒拉取一些数据。我知道当我调用
OnTime()
方法时,我需要传递与计划事件使用的相同的时间值。我还尝试引入多个命令(例如尝试引起错误)来停止执行,但仍然不起作用!程序仍然在运行...这是我的代码:
在工作表代码中:
Public TimerActive As Boolean
Public tick As String
Public idx As Long
Public counter As Long
Public Sub UpdateOff_Click()
TimerActive = False
tick = "Off"
counter = 1
idx = 1
Call StopTimer(idx, counter, tick, TimerActive)
End ' force quit??
End Sub
Public Sub UpdateOn_Click()
TimerActive = True
tick = "live"
counter = 1
idx = 1
Call StartTimer(idx, counter, tick, TimerActive)
End Sub
而在另一个模块中,我有:
Public fireTime As Date
Sub StartTimer(ByVal idx As Long, ByVal counter As Long, ByVal tick As String, ByVal TimerActive As Boolean)
fireTime = Now + TimeValue("00:00:05")
sMacro = " 'pullData " & idx & " , " & counter & ", " & Chr(34) & tick & Chr(34) & ", " & TimerActive & "'"
Application.OnTime EarliestTime:=fireTime, Procedure:=sMacro, Schedule:=True
End Sub
Sub StopTimer(ByVal idx As Long, ByVal counter As Long, ByVal tick As String, ByVal TimerActive As Boolean)
sMacro = "cause error" ' cause an error (by giving false sub name so program stops?
Application.OnTime EarliestTime:=fireTime, Procedure:=sMacro, Schedule:=False
End
End Sub
Public Sub pullData(ByVal idx As Long, ByVal counter As Long, ByVal tick As String, ByVal TimerActive As Boolean)
DoEvents
If TimerActive = True Then
' pull the data do some stuff, print the data, etc...
idx = idx + 1
counter = counter + 1
tick = tick + " ."
If counter = 6 Then
counter = 1
tick = "live"
End If
Call startTimer(idx, counter, tick, TimerActive)
End If
End Sub
我知道我可能增加了几个额外的措施来阻止执行,但似乎都没有奏效!