保存,关闭和重新打开文件后,VBA Excel计时器(秒表)时间将重置

时间:2017-08-04 17:10:54

标签: excel vba excel-vba

这只是我的第二篇文章,我希望我做得对。

我有一个工作簿,有几个计时器(秒表)工作正常。他们停止启动并清除活动的x'按钮没有问题。问题是当我关闭工作簿并重新打开时。我点击开始按钮,时间重置为00:00:00:00然后......这是我用来跟踪一个多月所花费的实际时间的事情,真的需要它不要重置为零之后关闭并重新开放。以下是我的代码..我是自学成才和借来的"大部分代码在线。提前致谢

Public StopIt As Boolean
Public ResetIt As Boolean
Public LastTime

Public StopIt2 As Boolean
Public ResetIt2 As Boolean
Public LastTime2


Private Sub CommandButton1_Click()
Dim StartTime, FinishTime, TotalTime, PauseTime

Me.CommandButton1.Enabled = False
StopIt = False
ResetIt = False
If Range("D5") = 0 Then
  StartTime = ("D5") = Timer
  PauseTime = 0
  LastTime = 0
Else
  StartTime = 0
  PauseTime = Timer
End If
StartIt:
  DoEvents
  If StopIt = True Then
    LastTime = TotalTime
    Exit Sub
  Else
    FinishTime = Timer
    TotalTime = FinishTime - StartTime + LastTime - PauseTime
    TTime = TotalTime * 100
    HM = TTime Mod 100
    TTime = TTime \ 100
    hh = TTime \ 3600
    TTime = TTime Mod 3600
    MM = TTime \ 60
    SS = TTime Mod 60
    Range("D5").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
    If ResetIt = True Then
      Range("D5") = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
      LastTime = 0
      PauseTime = 0

      End
    End If
    GoTo StartIt

  End If
End Sub

Private Sub CommandButton4_Click()
Dim StartTime2, FinishTime2, TotalTime2, PauseTime2

    Me.CommandButton4.Enabled = False
StopIt2 = False
ResetIt2 = False
If Range("D8") = 0 Then
  StartTime2 = Timer
  PauseTime2 = 0
  LastTime2 = 0
Else
  StartTime2 = 0
  PauseTime2 = Timer
End If
StartIt2:
  DoEvents
  If StopIt2 = True Then
    LastTime2 = TotalTime2
    Exit Sub
  Else
    FinishTime2 = Timer
    TotalTime2 = FinishTime2 - StartTime2 + LastTime2 - PauseTime2
    TTime = TotalTime2 * 100
    HM = TTime Mod 100
    TTime = TTime \ 100
    hh = TTime \ 3600
    TTime = TTime Mod 3600
    MM = TTime \ 60
    SS = TTime Mod 60
    Range("D8").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
    If ResetIt2 = True Then
      Range("D8") = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
      LastTime2 = 0
      PauseTime2 = 0
      End
    End If
    GoTo StartIt2
  End If
End Sub

Private Sub CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  Me.CommandButton1.Enabled = True
  StopIt = True
End Sub
Private Sub CommandButton5_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Me.CommandButton4.Enabled = True
    StopIt2 = True
End Sub

Private Sub CommandButton3_Click()
  Dim Msg As String, Ans As Variant

    Msg = "Are you sure you want to reset ALL timers .. Did you send your monthly report?"

    Ans = MsgBox(Msg, vbYesNo)

    Select Case Ans

    Case vbYes
  Range("D5").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
  LastTime = 0
  Range("D8").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
  LastTime2 = 0
  ResetIt = True

  Case vbNo
GoTo Quit:
    End Select

Quit:
End Sub

1 个答案:

答案 0 :(得分:0)

使用自定义属性存储持久数据

第一部分在sheet属性中存储数据,因此如果需要,您可以单独跟踪每个工作表

第二部分将数据存储在工作簿属性中 工作簿自定义属性也在"文件选项卡"中设置。在"属性"

B