Excel VBA秒表:添加暂停按钮?
问题描述:
我有一个带有开始/停止/清除按钮的秒表,它将时间发布到我的电子表格中的其他单元格。我想添加一个暂停按钮以允许用户暂停并恢复他们离开的计时器,但它会继续恢复,并将过渡时间加到总计中。例如,我点击开始并等待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
答
我觉得像这样的
Public blResume As Boolean
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")
If blResume = False Then Exit Sub
Application.OnTime NextTick, "StartTimer"
End Sub
Sub Pause()
blResume = False
End Sub
Sub myResume()
blResume = True
StartTimer
End Sub
+0
当我点击开始时它似乎没有运行。它是否在开始时意外地读取blresume为false?有没有一种方法可以在脚本运行之前将其设置为true,以便运行starttimer子版本? –
+0
设置它运行myresume。 –
哪里是你'子PauseTimer()'? – hellyale
我在暂停按钮的尝试中添加了。 –