Option Explicit
Dim StartTime
Dim EndTime
Dim RunAt
Dim oldStatBar
Public SessionTime
Public TotalTime
Const updateSecs = 1 'interval of seconds to update status bar
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' '
' Call StartProjTime from Private Sub Workbook_Open '
' '
' Call EndProjTime from Private Sub Workbook_BeforeClose '
' '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub UpdateProjTime()
StartTime = Evaluate("StartTime")
TotalTime = Evaluate("TotalTime")
EndTime = Now
SessionTime = EndTime - StartTime
Application.StatusBar = "Session Time: " & _
Application.WorksheetFunction.Text(SessionTime, "[h]:mm:ss") & _
" | Total Time: " & _
Application.WorksheetFunction.Text(TotalTime + SessionTime, "[h]:mm:ss")
TimerStart
End Sub
Sub StartProjTime()
If IsError(Evaluate("TotalTime")) Then ProjectTimeSetup
ThisWorkbook.Names("StartTime").RefersTo = Now
oldStatBar = Application.DisplayStatusBar
Application.DisplayStatusBar = True
UpdateProjTime
End Sub
Sub EndProjTime()
If IsError(Evaluate("TotalTime")) Then Exit Sub
UpdateProjTime
TimerStop
Application.StatusBar = False
Application.DisplayStatusBar = oldStatBar
ThisWorkbook.Names("TotalTime").RefersTo = TotalTime + SessionTime
End Sub
Private Sub TimerStart()
RunAt = Now + TimeSerial(0, 0, updateSecs)
Application.OnTime _
EarliestTime:=RunAt, _
Procedure:="UpdateProjTime"
End Sub
Private Sub TimerStop()
On Error Resume Next
Application.OnTime _
EarliestTime:=RunAt, _
Procedure:="UpdateProjTime", _
Schedule:=False
On Error GoTo 0
End Sub
Private Sub ProjectTimeSetup()
Dim arr As Variant, n As Variant
Dim response As String, fName
arr = Array("StartTime", "TotalTime")
For Each n In arr
If IsError(Evaluate(n)) Then _
ThisWorkbook.Names.Add Name:=n, RefersTo:=0
Next n
response = MsgBox("Create Template?", vbYesNo, "")
If response = vbNo Then Exit Sub
Do: fName = Application.GetSaveAsFilename
Loop Until fName <> False
ThisWorkbook.SaveAs Filename:=fName & "xltm", _
FileFormat:=xlOpenXMLTemplateMacroEnabled
SetAttr fName & "xltm", vbReadOnly
ThisWorkbook.Close False
End Sub