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

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

我有一个工作簿与几个定时器(停止手表)哪些工作正常。 他们停止开始,并与“活动x”button清除没有问题。 问题是当我closures工作簿并重新打开。 我点击开始button,时间重置为00:00:00:00,然后去…这是我用来追踪实际花费了一个多月的时间,真的需要它不closures后重置为零重新开放。 下面是我的代码..我自学成功,并在线上“借”了大部分代码。 提前致谢

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 

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

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

第二部分存储工作簿属性工作簿中的数据自定义属性也设置在“属性”下的“文件选项卡”

 Sub customProperties() ActiveWorkbook.Sheets("Sheet1").customProperties.Add "abc123", 123 Debug.Print ActiveWorkbook.Sheets("Sheet1").customProperties(1) ' custom properties are not indexed by name ActiveWorkbook.Sheets("Sheet1").customProperties(1).Value = "this is my data" Debug.Print ActiveWorkbook.Sheets("Sheet1").customProperties(1) ActiveWorkbook.Sheets("Sheet1").customProperties(1).Delete ' CustomDocumentProperties Types ' msoPropertyTypeBoolean 2 ' msoPropertyTypeDate 3 ' msoPropertyTypeFloat 5 ' msoPropertyTypeNumber 1 ' msoPropertyTypeString 4 ActiveWorkbook.CustomDocumentProperties.Add Name:="xyz", LinkToContent:=False, Type:=msoPropertyTypeString, Value:="xyz" Debug.Print ActiveWorkbook.CustomDocumentProperties("xyz") ActiveWorkbook.CustomDocumentProperties("xyz").Value = "abcdefg" Debug.Print ActiveWorkbook.CustomDocumentProperties("xyz") ActiveWorkbook.CustomDocumentProperties("xyz").Delete End Sub