【发布时间】:2017-06-16 20:35:05
【问题描述】:
我有一个带有开始/停止/清除按钮的秒表,可以将时间发布到电子表格中的其他单元格。我想添加一个暂停按钮,以允许用户暂停并恢复他们停止的计时器,但它会继续恢复并将中间经过的时间添加到总数中。例如,我点击开始并等待 5 分钟,然后在 5:00 点击暂停。我又等了 2 分钟,然后单击恢复,计时器在 7:00、7:01、7:02 等重新开始。如何创建有效的暂停/恢复按钮并告诉 excel 不包括经过的时间什么时候恢复计数?
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
【问题讨论】:
-
你的
Sub PauseTimer()在哪里? -
我在尝试中添加了一个暂停按钮。