此代码以前曾被用作非常稳定的倒数计时器,没有倒数错误(其他倒数代码有时会倒数为两倍或三倍计数)。
但是没有人知道如何暂停倒数计时器-例如,当人们在15分钟内休息时。然后,我希望计数器停止15分钟,然后从计数器暂停的地方重新启动。
当我使用Application.Wait
时,计时器仍在Do While Loop
的后面运行,并且不暂停。
Sub NewTimer()
Dim Start As Single
Dim Cell As Range
Dim CountDown As Date
Start = Timer
Set Cell = Sheet1.Range("B1") 'This is the starting value.
CountDown = TimeSerial(0, 3, 0) 'Set takttime
Cell.Value = CountDown
Do While Cell.Value > 0
'Update the cell. Timer - Start is the number of seconds
'that have elapsed since we set Start.
If Time >= TimeValue("08:45:00") And Time <= TimeValue("09:00:00") Then 'Pause in 15 minutes
Application.Wait (Now + TimeValue(Range"A14").Text))
ElseIf Time >= TimeValue("11:55:00") And Time <= TimeValue("12:25:00") Then 'Pause in 30 minutes
Application.Wait (Now + TimeValue(Range("A14").Text))
ElseIf Time >= TimeValue("14:00:00") And Time <= TimeValue("14:05:00") Then 'Pause in 5 minutes
Application.Wait (Now + TimeValue(Range("A14").Text))
Else: Cell.Value = CountDown - TimeSerial(0, 0, Timer - Start)
End If
DoEvents
Loop
End Sub