gpt4 book ai didi

Excel VBA秒表: add pause button?

转载 作者:行者123 更新时间:2023-12-04 20:34:33 25 4
gpt4 key购买 nike

我有一个带有开始/停止/清除按钮的秒表,可以将时间发布到电子表格中的其他单元格。我想添加一个暂停按钮,以允许用户暂停并恢复他们停止的计时器,但它会继续恢复并将中间耗时添加到总数中。例如,我点击开始并等待 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

最佳答案

我是这样想的

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

关于Excel VBA秒表: add pause button?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44597573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com