使用 OnTime 宏停止潜艇的按钮不会停止

Button to stop subs with OnTime Macro doesn't stop

我有一个按钮分配给 StopRecordingData 子来取消两个子,但它没有。 schedule False 似乎没有取消 que 中的预定 subs。

Dim NextTime As Double

Sub RecordData()
    Dim Interval As Double
    Dim cel As Range, Capture As Range

    Application.StatusBar = "Recording Started"
    Set Capture = Worksheets("Dashboard").Range("C5:K5") 'Capture this row 
    of data
    With Worksheets("Journal") 'Record the data on this worksheet
    Set cel = .Range("A2") 'First timestamp goes here
    Set cel = .Cells(.Rows.Count, cel.Column).End(xlUp).Offset(1, 0)
    cel.Value = Now
    cel.Offset(0, 1).Resize(1, Capture.Cells.Count).Value = Capture.Value
    End With
    NextTime = Now + TimeValue("00:01:00")
    Application.OnTime NextTime, "CloseWB"
End Sub

Sub CloseWB()
    Application.OnTime NextTime, "RecordData"
    ThisWorkbook.Close True
End Sub

Sub StopRecordingData()
    Application.StatusBar = "Recording Stopped"
    Application.OnTime NextTime, "RecordData", False
    Application.OnTime NextTime, "CloseWB", False
End Sub

你必须

  • 最后使用 2 个逗号或
  • OnTime 方法的完整方法定义

因为 OnTime 方法的语法有 4 个参数,最后 2 个是可选的。

Application.OnTime EarliestTime, Procedure, [LatestTime], [Schedule]

此外,对于与时间相关的变量,更喜欢 DATE 而不是 DOUBLE。因此在顶部使用它。

Dim NextTime as Date

Sub StopRecordingData()
    Application.StatusBar = "Recording Stopped"
    Application.OnTime NextTime, "RecordData",, False
    Application.OnTime NextTime, "CloseWB",, False
End Sub

Sub StopRecordingData()
    Application.StatusBar = "Recording Stopped"
    Application.OnTime EarliestTime:=NextTime, Procedure:="RecordData", Schedule:=False
    Application.OnTime EarliestTime:=NextTime, Procedure:="CloseWB", Schedule:=False
End Sub