我有一個帶有開始/停止/清除按鈕的秒錶,它將時間發佈到我的電子表格中的其他單元格。我想添加一個暫停按鈕以允許用戶暫停並恢復他們離開的計時器,但它會繼續恢復,並將過渡時間加到總計中。例如,我點擊開始並等待5分鐘,然後點擊5:00暫停。我再等2分鐘,然後點擊恢復,計時器在7點,7點1分,7點2分等時間恢復。如何創建有效的暫停/恢復按鈕,並告訴excel不要包含已通過的時間什麼時候恢復計數?Excel VBA秒錶:添加暫停按鈕?
Dim NextTick As Date, t As Date
Sub StartStopWatch()
t = Time
Call StartTimer
End Sub
Sub StartTimer()
NextTick = Time + TimeValue("00:00:01")
Range("M1").Value = Format(NextTick - t - TimeValue("00:00:01"), "hh:mm:ss")
Application.OnTime NextTick, "StartTimer"
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=NextTick, Procedure:="StartTimer", Schedule:=False
End Sub
Sub ResetTimer()
Range("M1").ClearContents
Range("N1").ClearContents
Range("L2").ClearContents
End Sub
Sub PauseButton()
If Range("L2").Value = "" Then
Range("L2").Value = "Paused"
Application.OnTime EarliestTime:=NextTick, Procedure:="StartTimer", Schedule:=False
Else
Range("L2").ClearContents
Application.OnTime EarliestTime:=NextTick, Procedure:="StartTimer", Schedule:=True
End If
End Sub
哪裏是你'子PauseTimer()'? – hellyale
我在暫停按鈕的嘗試中添加了。 –