Vba macro [stopwatch] stopping when inputting cell

Yermaw

New Member
Joined
Nov 19, 2016
Messages
1
Hi,

I have a stop watch running in my Excel spreadsheet, I need it to running continuously throughout my session. Currently it stops when I input new data into cell. Can anyone help to make this run no matter what? Here's the code:

Code:
Public StopIt As Boolean
Public ResetIt As Boolean
Public LastTime
Private Sub CommandButton1_Click()
Dim StartTime, FinishTime, TotalTime, PauseTime
StopIt = False
ResetIt = False
If Range("C2") = 0 Then
  StartTime = 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("C2").Value = Format(hh, "00") & ":" & Format(MM, "00") & ":" & Format(SS, "00") & "." & Format(HM, "00")
    If ResetIt = True Then
      Range("C2") = 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 CommandButton2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  StopIt = True
End Sub
Private Sub CommandButton3_Click()
  Range("C2").Value = Format(0, "00") & ":" & Format(0, "00") & ":" & Format(0, "00") & "." & Format(0, "00")
  LastTime = 0
  ResetIt = True
End Sub
[CODE/]
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Here's some code that may point you in a direction.

Code:
Option Explicit


Const captionStart = "Start"
Const captionStop = "Stop"
Const captionTimer = "Time elapsed in seconds."


Public startTime As Double
Public endTime As Double


Private Sub bttnStopWatch_Click()
    If bttnStopWatch.Caption = captionStart Then
        startTime = Timer
        bttnStopWatch.Caption = captionStop
    Else
        endTime = Timer
        If IsNumeric(lblSeconds.Caption) Then
            lblSeconds.Caption = lblSeconds.Caption + (endTime - startTime)
        Else
            lblSeconds.Caption = endTime - startTime
        End If
        bttnStopWatch.Caption = captionStart
    End If
End Sub


Private Sub lblSeconds_Click()
    If MsgBox("Reset Stop Watch?", vbYesNo) = vbYes Then
        lblSeconds.Caption = captionTimer
    End If
End Sub


Private Sub UserForm_Initialize()
    bttnStopWatch.Caption = captionStart
    lblSeconds.Caption = captionTimer
End Sub

It's a form with a button and a label. Button Starts/Stops and the Label shows the elapsed time. Click the label and the timer will reset.
 
Upvote 0

Forum statistics

Threads
1,215,686
Messages
6,126,202
Members
449,298
Latest member
Jest

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top