如何让计时器从上次停止的时间开始,而不是每次都重置

时间:2015-10-24 21:13:18

标签: excel vba timer

在A栏中输入一个值。 首先,双击A列中的值。 要停止,请双击任何空白单元格。 停止在B2单元格中运行

Public stopMe As Boolean
Public resetMe As Boolean
Public myVal As Variant

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 1 Then
        If Target.Value = myVal And Target.Value <> "" Then
            'Changed
            Dim startTime, finishTime, totalTime, timeRow
            startTime = Timer
            stopMe = False
            resetMe = False
            myTime = Target.Offset(, 2).Value
            Target.Offset(, 1).Select
startMe:
            DoEvents
            timeRow = Target.Row
            finishTime = Timer
            totalTime = finishTime - startTime
            Target.Offset(, 1).Value = Format(800 + totalTime, "0000")
            If resetMe = True Then
                Target.Offset(, 1).Value = 0
                Target.Offset(, 2).Value = 0
                stopMe = True
            End If
            If Not stopMe = True Then
                Target.Offset(, 2).Value = totalTime
                GoTo startMe
            End If
            Cancel = True
            End
        Else
            'Not Changed
            stopMe = True
            Cancel = True
        End If
    End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    myVal = Target.Value
End Sub

如何让计时器从上次停止的时间开始,而不是每次都重置

2 个答案:

答案 0 :(得分:1)

我认为这对你有用。

Option Explicit
Dim CmdStop As Boolean
Dim Paused As Boolean
Private Sub btnStart_Click()
    CmdStop = False
    Paused = False
    Dim TimerValue As Date
    Dim pausedTime As Date
    Start = Now()    ' Set start time.
    btnPause.Enabled = True
    btnStop.Enabled = True
    btnReset.Enabled = False
    Do While CmdStop = False
        If Not Paused Then
            TimerValue = Now() - Start - pausedTime
        Else
            pausedTime = Now() - TimerValue - Start
        End If
        TimerReadOut.Caption = Format(TimerValue, "h:mm:ss")
        DoEvents    ' Yield to other processes.
    Loop
End Sub
Private Sub btnPause_Click()
    If btnPause.Caption = "Pause" Then
        Paused = True
        btnPause.Caption = "Continue"
    Else
        Paused = False
        btnPause.Caption = "Pause"
    End If

End Sub
Private Sub BtnReset_Click()
    TimerReadOut.Caption = "0:00:00"
    btnStop.Enabled = False
End Sub
Sub BtnStop_Click()
    btnPause.Enabled = False
    btnReset.Enabled = True
    btnStop.Enabled = False
    CmdStop = True
End Sub

答案 1 :(得分:0)

尝试将停留时间写入某个特定Excel工作表上的单元格,然后在下次启动时选择该编号。这将为您提供可用于不同开始的全局值。