无法使Pause Button正常恢复时间 - Excel VBA

时间:2017-06-21 15:03:55

标签: excel vba button timer pause

我在Excel中编写一个秒表计时器,我正在尝试添加一个停止计时器的暂停按钮,第二次点击时也会恢复。我几乎可以工作,但是当它恢复时它不会同时继续秒表,而是包括所有经过的时间。我的工作方式是,当你点击暂停按钮时,它会增加" Paused"的值。到一个单元格并将应用程序设置为" False"。如果单击该按钮并且单元格已经显示"已暂停"它将清空单元格值并调用StartTimer子并再次启动计时器。

问题示例:如果我在1分钟和3秒钟按下按钮,等待3分钟并恢复计时器,它会在4分钟和3秒后重新开始计时。

目标示例:如果我在1分钟和3秒钟按下按钮,等待3分钟并恢复计时器,它应该在1分钟和4秒后再次开始计数。

以下是我目前正在处理的代码:

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
Call StartTimer

End If

End Sub

3 个答案:

答案 0 :(得分:0)

暂停时,您的代码不会重置开始时间。但重置开始时间会丢失之前的经过时间。解决此问题的一种方法是记录每次时钟停止时经过的秒数。子序列开始/停止将添加到正在运行的计数器中。有一个重置方法,所以你可以从零开始。

Private StartTime As Date           ' Stores timer start time.
Private StopTime As Date            ' Stores timer end time.
Private ElapsedTime As Integer      ' Store number of seconds between start and end time.
Private TimerRunning As Boolean     ' True when the timer is running.


' Resets ElapsedTime and running status.
Public Sub ResetTimer()

    ElapsedTime = 0
    If TimerRunning Then TimerRunning = False
End Sub

' Starts the timer.
Public Sub StartTimer()

    ' Record start time.
    StartTime = Time
    TimerRunning = True
End Sub

' Stops the timer.
' Displays elapsed seconds.
Public Sub StopTimer()

    ' Record end time.
    StopTime = Time
    TimerRunning = False

    ' Calculate numebr of seconds clock was running for.
    ' Add total to any previous runs.
    ElapsedTime = ElapsedTime + DateDiff("S", StartTime, StopTime)
    MsgBox "Clock ran for " & ElapsedTime & " seconds", vbInformation, "Clock Stopped"
End Sub

' Pause the timer.
Public Sub PauseTimer()

    ' Check current status.
    If TimerRunning Then

        ' Stop the clock, capture the end time
        ' but do not reset the timer.
        StopTimer
    Else

        ' Restart the timer.
        StartTimer
    End If
End Sub

用户只需要访问暂停和重置功能。您可以在内部private methods内启动和停止功能。

我已将经过的时间存储为整数。应使用日期和时间变量来记录事件发生的时间,而不是持续时间。为什么?因为时间变量支持来自00:00:00 - >的值。 23:59:59(一天的时间)。这样就没有余地记录持续时间超过一天的事件。您可以在显示时始终格式化int。

答案 1 :(得分:0)

我不知道你的代码究竟是如何工作的,我尝试了几件事,但它没有用。基本上我想要的是在下次启动时从细胞M1本身抽出时间,然后从那里继续。看看这段代码,也许会有所帮助。

Dim NextTick As Date, t As Date, continue_time As Date
Sub StartStopWatch()
If Range("L2").Value = "" Then
    t = Time
    Range("L2").ClearContents
Else
    t = TimeValue(Range("M1").Text) - Time
End If
't = Time
Call StartTimer
End Sub

Sub StartTimer()
'If Range("L2").Value = "" Then
'    continue_time = Time
'Else
'    continue_time = TimeValue(Range("M1").Text)
'End If

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

Call StartStopWatch

End If

End Sub

答案 2 :(得分:0)

我实际上只是使用一个计算整数的代码,然后将格式更改为mm:ss。

解决方案:http://www.ozgrid.com/forum/showthread.php?t=153966