我有一个 VBA 宏,它使用“开始”、“停止”和“重置”按钮在我单击的每个单元格上启动一个计时器。但是,当我单击工作表上的另一个单元格(从而更改活动单元格)时,前一个单元格上的计时器会自动停止。
我希望它继续运行,直到我使用停止按钮告诉它停止。
我在下面发布了代码,希望你们可以看看,并可能帮助这个新手:)
Dim StopTimer As Boolean
Dim SchdTime As Date
Dim Etime As Date
Const OneSec As Date = 1 / 86400#
Private Sub StartBtn_Click()
Etime = 0
Selection.Interior.ColorIndex = 6
StopTimer = False
SchdTime = Now()
Selection.Value = Format(Etime, "hh:mm:ss")
Application.OnTime SchdTime + OneSec, "Sheet1.NextTick"
End Sub
Private Sub ResetBtn_Click()
Selection.Interior.ColorIndex = -4142
StopTimer = True
Etime = 0
Selection.Value = "00:00:00"
End Sub
Private Sub StopBtn_Click()
Selection.Interior.ColorIndex = 4
StopTimer = True
Beep
End Sub
Sub NextTick()
If StopTimer Then
'Dont Reschedule Update
ElseIf Selection.Interior.ColorIndex = 6 Then
Selection.Value = Format(Etime, "hh:mm:ss")
SchdTime = SchdTime + OneSec
Application.OnTime SchdTime, "Sheet1.NextTick"
Etime = Etime + OneSec
End If
End Sub