2017-08-04 54 views
1

這只是我的第二篇文章,我希望我說得對。VBA Excel定時器(秒錶)時間在保存,關閉並重新打開文件後重置

我有一個工作簿與幾個定時器(停止手錶)哪些工作正常。他們停止開始並用「活動的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

您將需要保存t他目前在工作簿中的某處,例如工作簿關閉時作爲NAME(Google它),然後在開始備份時從名稱中重新載入「計時器」。 – hoodaticus

+0

你會希望將值保存爲INI文件(這只是一個奇特的文本文件) - 但說實話,你真的應該使用數據庫這樣的東西。你有沒有Microsoft Access? – braX

+0

我有訪問,但不幸的是零經驗使用它。 –

回答

0

存儲持久性數據

在表屬性的第一部分存儲數據,所以你可以單獨跟蹤每個工作表的,如果你想

第二部分使用自定義屬性將數據存儲在工作簿屬性中 工作簿定製屬性也在「屬性」下的「文件選項卡」中設置

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 
相關問題