VBA - Timer Functions

Rana Gray

Board Regular
Joined
Jan 26, 2023
Messages
53
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hello Lovely Excel Experts!! I am here again for your vast wisdom.

I have coded a timer (using a userform) it works beautifully I am just looking to add a feature if possible. Userforms as far as I know don't save data once the file is closed so what I need to figure out is how to allow for a timer "pause" and "continue" from the same place even after the file has been closed and re-opened. I am happy to have the data stored somewhere on the sheet, just curious if this is possible. See code below and screengrab of the timer userform & sheet for visuals

UserForm1
VBA Code:
Option Explicit
Dim CmdStop As Boolean
Dim Paused As Boolean
Dim Start
Dim TimerValue As Date
Dim pausedTime As Date

Sub btnStart_Click()

btnPause.Enabled = True

CmdStop = False
Paused = False
Start = Now() ' Set start time.
btnPause.Enabled = True
btnStop.Enabled = True
btnReset.Enabled = False
Do While CmdStop = False
If Not Paused Then
TimerValue = Now() - Start - pausedTime
Else
pausedTime = Now() - TimerValue - Start
End If
TimerReadOut = Format(TimerValue, "h:mm:ss")
DoEvents ' Yield to other processes.
Loop
End Sub

Sub btnPause_Click()
If btnPause.Caption = "Pause" Then
Paused = True
btnPause.Caption = "Continue"
Else
Paused = False
btnPause.Caption = "Pause"
End If
UserForm1.Hide

End Sub

Sub BtnReset_Click()
TimerReadOut = "0:00:00"
btnStop.Enabled = False
End Sub
Sub BtnStop_Click()
btnPause.Enabled = False
btnReset.Enabled = True
btnStop.Enabled = False
CmdStop = True

Sheet1.Range("M8").Value = TimerReadOut.Value
If TimerReadOut.Value = "" Then Sheet1.Range("M9").Value = ""
UserForm1.Hide

End Sub

Private Sub BtnReset_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Shift = 0 Then
    Range("N8") = Range("N8") + 1
    Else: Range("N8") = Range("N8") - 1
End If
End Sub
_________________________________

Thanks a MILLION in advance!!

1694625963046.png


1694626000567.png
 
Last edited by a moderator:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

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