I'm trying to create a three (or more) -lane stopwatch in Excel and I'm not getting expected results. A single Start button launches a macro that loops through a series of checks to see if any of the lane stop buttons have been clicked and update the running time in three cells. If a lane stop button is clicked, the time for that lane should stop incrementing but the other two will continue to update. I have some other things I want to do with this like resume a lane after a stop (not including the stopped time) and continuing a lane after a stop but still accumulating the time during the stop. For now, though, I'd just like to get this much working.
The start button works to start the displays for all three lanes. Clicking start again while the subroutine is running, restarts all time displays from zero.
The reset button stops all lane displays but only resets the values to zero after a second click. It does not end the subroutine which continues to run until I hit Run ~ Reset in the vb editor. Occasionally, if I het reset while the subroutine is running, the three displays jump to a completely unexpected time value. (Start, 2 seconds later, Reset, all displays show 16:38)
The three stop buttons stop all three lanes together regardless of which stop button is clicked.
I'll need to add an exit button, too, to end the subroutine without clearing the accumulated values.
Can someone please show me the error of my ways or is this just not the way to go about this? Thanks for any help.
This is my code so far. It includes snippets I've found in various articles and examples:
The start button works to start the displays for all three lanes. Clicking start again while the subroutine is running, restarts all time displays from zero.
The reset button stops all lane displays but only resets the values to zero after a second click. It does not end the subroutine which continues to run until I hit Run ~ Reset in the vb editor. Occasionally, if I het reset while the subroutine is running, the three displays jump to a completely unexpected time value. (Start, 2 seconds later, Reset, all displays show 16:38)
The three stop buttons stop all three lanes together regardless of which stop button is clicked.
I'll need to add an exit button, too, to end the subroutine without clearing the accumulated values.
Can someone please show me the error of my ways or is this just not the way to go about this? Thanks for any help.
This is my code so far. It includes snippets I've found in various articles and examples:
VBA Code:
'Option Explicit
Public Stop1 As Boolean
Public Stop2 As Boolean
Public Stop3 As Boolean
Public ResetAll As Boolean
Dim Lane1 As Boolean
Dim Lane2 As Boolean
Dim Lane3 As Boolean
Private Sub StartButton_Click()
Stop1 = False ' all stop flags are off
Stop2 = False
Stop3 = False
ResetAll = False
Lane1 = True ' all lanes are active
Lane2 = True
Lane3 = True
StartTime = Timer ' start time is set
StartTime1 = StartTime
StartTime2 = StartTime
StartTime3 = StartTime
Do
DoEvents
NowTime = Timer
If Stop1 = True Then
StopTime1 = NowTime
TotalTime1 = StopTime1 - StartTime1
DisplayTime1 = TotalTime1
Lane1 = False
ElseIf Stop2 = True Then
StopTime2 = NowTime
TotalTime2 = StopTime2 - StartTime2
DisplayTime2 = TotalTime2
Lane2 = False
ElseIf Stop3 = True Then
StopTime3 = NowTime
TotalTime3 = StopTime3 - StartTime3
DisplayTime1 = TotalTime3
Lane3 = False
Else
If Lane1 = True Then DisplayTime1 = NowTime - StartTime1 ' change the displayed time only if the lane is still active
If Lane2 = True Then DisplayTime2 = NowTime - StartTime2
If Lane3 = True Then DisplayTime3 = NowTime - StartTime3
TTime = DisplayTime1 * 100 ' update the displayed time (stopped or active) for each lane
HM = TTime Mod 100
TTime = TTime \ 100
hh = TTime \ 3600
TTime = TTime Mod 3600
MM = TTime \ 60
SS = TTime Mod 60
Range("G2").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
TTime = DisplayTime2 * 100
HM = TTime Mod 100
TTime = TTime \ 100
hh = TTime \ 3600
TTime = TTime Mod 3600
MM = TTime \ 60
SS = TTime Mod 60
Range("G3").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
TTime = DisplayTime3 * 100
HM = TTime Mod 100
TTime = TTime \ 100
hh = TTime \ 3600
TTime = TTime Mod 3600
MM = TTime \ 60
SS = TTime Mod 60
Range("G4").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
If ResetAll = True Then Exit Sub
End If
Loop
End Sub
Private Sub StopButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Stop1 = True
End Sub
Private Sub StopButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Stop2 = True
End Sub
Private Sub StopButton3_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Stop3 = True
End Sub
Private Sub ResetButton_Click()
Range("G2").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
Range("G3").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
Range("G4").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
ResetAll = True
End Sub