MsDeth
New Member
- Joined
- May 8, 2004
- Messages
- 41
Have a simple time recorder with 3 buttons that start, pause and reset the clock.
It works quite well EXCEPT that if I open/start working on another Excel file, the clock starts on the new file.
The "Time Elapsed" number pauses when I switch to another file, but continues working. When I return to that file the number updates.
The shown files are off by a second because of my timing, in reality they run together.
I've tried everything I can think of-including adding the workbook & worksheet names to the VBA.
What am I missing? (Much thanks in advance.)
Here is the basic code, based on one written by Philip Treacy, My Online Training Hub
'Timer or Stopwatch in Excel VBA
Option Explicit
Sub StartTimer2()
Dim Start As Single, RunTime As Single, CurrentlyElapsed As Single
Dim ElapsedTime As String
Range("F2").Value = 0
Range("D2").Interior.Color = 5296274 'Green
Start = Timer ' Set start time.
Do While Range("F2").Value = 0
DoEvents ' Yield to other processes.
RunTime = Timer ' current elapsed time
CurrentlyElapsed = RunTime - Start + Range("G2").Value
ElapsedTime = Format(CurrentlyElapsed / 86400, "hh:mm:ss")
Range("D2").Value = ElapsedTime
Application.StatusBar = ElapsedTime
Loop
Range("D2").Value = ElapsedTime
Range("D2").Interior.Color = 65535 'yellow
Range("G2").Value = CurrentlyElapsed
Application.StatusBar = False
End With
End Sub
--------------------
Sub StopTimer2()
'Set the control cell to 1
Range("F2").Value = 1
End Sub
--------------------
Sub ResetTimer2()
If Range("F2").Value > 0 Then
'Set the control cell to 1
Range("D2").Interior.Color = 16119285 'smoke
Range("D2").Value = Format(0, "hh:mm:ss")
Range("G2").Value = 0
End If
End Sub