Option Explicit
' Written by Philip Treacy, My Online Training Hub
' https://www.myonlinetraininghub.com/timer-stopwatch-excel-vba
Public Start As Variant
Sub StartTimer()
Dim Start As Variant, RunTime As Variant, CurrentlyElapsed As Variant
Dim ElapsedTime As String
If Not ActiveWorkbook Is ThisWorkbook Then Exit Sub
'Set the control cell to 0 and make it green
Range("B1").Value = 0
Range("C4").Interior.Color = 13561798 'Light Green
Range("C4").Font.Color = 24832 'Dark Green
'If Range("D1").Value <> 0 Then CurrentlyElapsed = Range("D1").Value
Start = Time - Range("C4").Value ' Set start time subtract existing count
Debug.Print Start
Do While Range("B1").Value = 0
DoEvents ' Yield to other processes.
' RunTime = Timer ' current elapsed time
' CurrentlyElapsed = RunTime - Start + Range("D1").Value
CurrentlyElapsed = Time - Start
' ElapsedTime = Format(CurrentlyElapsed / 86400, "hh:mm:ss")
ElapsedTime = Format(CurrentlyElapsed, "hh:mm:ss")
' ElapsedTime = Format(CurrentlyElapsed / 86400, "hh:mm")
'Display currently elapsed time in C4
Range("C4").Value = ElapsedTime
Application.StatusBar = ElapsedTime
Loop
Range("C4").Value = ElapsedTime
Range("C4").Interior.Color = 13551615 'light Red
Range("C4").Font.Color = 393372 'Dark Red
Range("D1").Value = CurrentlyElapsed
Application.StatusBar = False
End Sub
Sub StopTimer()
If Not ActiveWorkbook Is ThisWorkbook Then Exit Sub
'Set the control cell to 1
Range("B1").Value = 1
Range("C4").Interior.Color = 13551615 'light Red
Range("C4").Font.Color = 393372 'Dark Red
End Sub
Sub ResetTimer()
Dim myRange As Range
Dim CopyRange As Range
Dim answer As Integer
If Not ActiveWorkbook Is ThisWorkbook Then Exit Sub
Set CopyRange = Range("C4")
On Error Resume Next
answer = MsgBox("Would you like to capture this time for one of the listed task?", vbQuestion + vbYesNo + vbDefaultButton2, "Capture or Clear Time Shown")
If answer = vbYes Then
' MsgBox "Yes"
Set myRange = Application.InputBox(Prompt:="Select Cell you want to capture your total time in.", Title:="Format Titles", Type:=8)
If myRange Is Nothing Then
MsgBox "No selection made", vbCritical, "Input required"
Exit Sub
End If
CopyRange.Value = (CopyRange * 24 * 60 * 60) / 3600
myRange.Value = CopyRange.Value
Else
' MsgBox "No"
End If
If Range("B1").Value > 0 Then
'Set the control cell to 1
Range("C4").Value = Format(0, "hh:mm:ss")
' Range("C4").Value = Format(0, "hh:mm")
Range("C4").Interior.Color = 13551615 'light Red
Range("C4").Font.Color = 393372 'Dark Red
Range("D1").Value = 0
End If
End Sub
Sub ResumeOrRestart()
Dim Result As VbMsgBoxResult
If Not ActiveWorkbook Is ThisWorkbook Then Exit Sub
Result = MsgBox("Select 'Yes' to resume from your previous clocked time. Select 'No' to automatically 'RESET' the 'Stopwatch'", vbYesNo + vbQuestion)
If Result = vbYes Then
'MsgBox "You clicked Yes"
If Range("B1").Value > 0 Then
'Set the control cell to 1
Range("C4").Value = Format(0, "hh:mm:ss")
'Range("C4").Value = Format(0, "hh:mm")
Range("C4").Interior.Color = 13551615 'light Red
Range("C4").Font.Color = 393372 'Dark Red
End If
Range("C4").Value = Range("D1").Value
Else:
'MsgBox "You clicked No"
If Range("B1").Value > 0 Then
'Set the control cell to 1
Range("C4").Value = Format(0, "hh:mm:ss")
'Range("C4").Value = Format(0, "hh:mm")
Range("C4").Interior.Color = 13551615 'light Red
Range("C4").Font.Color = 393372 'Dark Red
Range("D1").Value = 0
End If
Range("C4").Value = Range("E1").Value
End If
End Sub