Timer in VBA - Stop, Start, Pause and Reset

Anton1999

New Member
Joined
Aug 5, 2020
Messages
25
Office Version
  1. 2013
Platform
  1. Windows
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()

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.Caption = 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

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


Can someone PLEASE PLEASE assist, keeps on saying that I have not declared a variable on all macros? The buttons are not working and I have assigned the macros to them.
I attached my workbook as well to show my buttons.

What am I missing?
 

Attachments

  • Screenshot of my workbook.png
    Screenshot of my workbook.png
    39.2 KB · Views: 266
This is the code that I wrote for the start/stop/reset program that works with the 3 buttons on my sheet.(they are just shapes that were assigned a macro).
The timer is working but cannot pause

Option Explicit
Sub StartTimer()
'Setting the procedures to assign

Dim Start As Single, RunTime As Single
Dim ElapsedTime As String

'Set the control cell to 0 and make it green
Range("C1").Value = 0
Range("A1").Interior.Color = 5296274 'Green

Start = Timer ' Set start time.
Debug.Print Start
Do While Range("C1").Value = 0

DoEvents ' Yield to other processes.
RunTime = Timer ' current elapsed time
ElapsedTime = Format((RunTime - Start) / 86400, "hh:mm:ss")
'Display currently elapsed time in A1
Range("A1").Value = ElapsedTime
Application.StatusBar = ElapsedTime

Loop

Range("A1").Value = ElapsedTime
Range("A1").Interior.Color = 192 'Dark red
Application.StatusBar = False

End Sub

Sub PauseTimer()
'Pause the timer to add the time to each instance
StartTimer.Pause




End Sub

Sub StopTimer()

'Set the control cell to 1
Range("C1").Value = 1

End Sub

'Procedure to be execuetd when the button reset is done
Sub ResetTimer()
Dim show As Variant

'When the timer is reset display the time so that it could be added together as a sum for our time budgets
show = Range("A1").Value

If Range("C1").Value > 0 Then

'Set the control cell to 1
Range("A1").Value = Format(0, "hh:mm:ss")
Range("D4").Value = show

End If

End Sub

Sub StartCountdown()

Dim ZeroHour As Date
Dim TimeDifference As Single

'Set the control cell to 0 and make it green
Range("C6").Value = 0
Range("A6").Interior.Color = 5296274 'Green
ZeroHour = Range("A5").Value ' Set start time.
TimeDifference = ZeroHour - Now

If TimeDifference <= 0 Then

Range("A6").Value = "Error : date/time in the past"
StopCountdown
Exit Sub

End If


Do While Range("C6").Value = 0 And TimeDifference > 0

DoEvents ' Yield to other processes.
TimeDifference = ZeroHour - Now
Range("A6").Value = Int(TimeDifference) & "d " & Format((TimeDifference), "hh:mm:ss")
Application.StatusBar = Int(TimeDifference) & "d " & Format((TimeDifference), "hh:mm:ss")

Loop

Range("A6").Interior.Color = 192 'Dark red
Application.StatusBar = False

End Sub

Sub StopCountdown()

'Set the control cell to 1
Range("C6").Value = 1

End Sub

Sub ResetCountdown()

If Range("C6").Value > 0 Then

'Set the control cell to 1
Range("A6").Value = "0 d " & Format(0, "hh:mm:ss")

End If

End Sub
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I will be creating round about 50 buttons in the end. To record the different sections of my work in real-time.

I think you may be making this more complicated than necessary

What exactly are you trying to achieve?
Do you really need a timer running?

SUGGESTION
if all you want is the time spent on a job, simply use
- a dropdown to select the job
- button START to create a timestamp when you start the job
- button STOP to create a timestamp at the end
- the difference can easily be calculated by Excel
- button PAUSE is not required (unless that time is to be captured) - click STOP and then START (without changing the job) when ready
 
Upvote 0
I think you may be making this more complicated than necessary

What exactly are you trying to achieve?
Do you really need a timer running?

SUGGESTION
if all you want is the time spent on a job, simply use
- a dropdown to select the job
- button START to create a timestamp when you start the job
- button STOP to create a timestamp at the end
- the difference can easily be calculated by Excel
- button PAUSE is not required (unless that time is to be captured) - click STOP and then START (without changing the job) when ready


See I work for a tax, audit and accounting firm. We do audits etc. So a lot of different tasks like auditing expenses or revenue have to be done while multi-tasking different other work. Juniors interrupting etc. I then need to cease one task and immediately start with another. So a start pause would work so good. We also need to show these time in real-time when presenting our audit file to the IRBA. Also a lot of our staff cannot even comprehend majority Excel functions. So to compile an pre-programmed user-friendly workbook for all the different engagements and prevent human error as far possible. So a template.

If I may ask, could you assist me in programming a pause button for my previous message?
 
Upvote 0
So more than one job will be active. I wanted to use drop down lists, but that takes to much time. So a table with all tasks with timers next to the tasks would work perfect. If I have the main code it would be easy to assign them to other buttons.
 
Upvote 0
TimerReadOut
- is it an active-x object or something else ?
- what type of object is it?

thanks
 
Upvote 0
I seem to have fixed the issue, everything works now. I just need to generate more buttons etc. hehe
 
Upvote 0
Glad you have everything working (y)
Good luck
 
Upvote 0
Belt and braces ...
Other processes and tabbing to other applications may interrupt your timer.

HINT
Double-check that the timer is giving consistently accurate results for your record keeping
Consider creating a separate sheet to timestamp every button click
 
Upvote 0
Belt and braces ...
Other processes and tabbing to other applications may interrupt your timer.

HINT
Double-check that the timer is giving consistently accurate results for your record keeping
Consider creating a separate sheet to timestamp every button click
Good afternoon,

Could you maby assist me. I have more than one Excel workbook open, the macros I created in module 1 in my timer workbook
records over all the other Excels as well. It should just work on the one Excel I created it. Not all the other workbooks as well.

How do I fix this?
 
Upvote 0

Forum statistics

Threads
1,216,175
Messages
6,129,311
Members
449,499
Latest member
HockeyBoi

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