Code on Sheet1 :
Private Sub CommandButton1_Click()
Timer_Start
End Sub
Private Sub CommandButton2_Click()
Timer_Stop
End Sub
Code on Sheet2 :
Private Sub CommandButton1_Click()
Countdown_Start
End Sub
Code on Module1 :
Option Private Module
Private Timer_isContinue As Boolean, Timer_StartTime As Date, Countdown_Continue As Boolean, Countdown_TargetTime As Date
Sub Timer_Start()
Sheet1.CommandButton1.Enabled = False
Sheet1.CommandButton2.Enabled = True
Timer_isContinue = True
Timer_StartTime = Now
Timer_Update
End Sub
Sub Timer_Stop()
Timer_isContinue = False
Sheet1.Range("IV1").End(xlToLeft).Offset(, 1).Value = Sheet1.Range("A1").Value
Sheet1.CommandButton1.Enabled = True
Sheet1.CommandButton2.Enabled = False
End Sub
Sub Timer_Update()
If Timer_isContinue Then
Sheet1.Range("A1").Value = Format$(Hour(Now - Timer_StartTime), "00") & " h " & Format$(Minute(Now - Timer_StartTime), "00") & " m " & Format$(Second(Now - Timer_StartTime), "00") & " s"
Application.OnTime Now + TimeValue("00:00:01"), "Timer_Update"
End If
End Sub
Sub Countdown_Start()
Sheet2.CommandButton1.Enabled = False
Countdown_TargetTime = Now + Sheet2.Range("A1").Value
Countdown_Update
End Sub
Sub Countdown_Update()
Sheet2.Range("A1").Value = Application.Max(Countdown_TargetTime - Now, 0)
If Countdown_TargetTime - Now <= 0 Then
Countdown_Continue = False
Sheet2.CommandButton1.Enabled = True
MsgBox "Countdown time has been reached"
Else
Application.OnTime Now + TimeValue("00:00:01"), "Countdown_Update"
End If
End Sub
Bookmarks