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: 255

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
welcome to MrExcel

the reason you are getting the message is that vba cannot find these variables
btnPause
btnStop
btnReset

Of course, they are not variables, they are buttons - but VBA has not been told where to find them and so VBA assumes they are variables - hence the message

Are they active-x command buttons?
- reference required to active sheet like this
Rich (BB code):
ActiveSheet.btnPause = True


If you require further help ...
- where is your code located ? In a module ? In sheet code ?
 
Last edited:
Upvote 0
My code is located in a module, not in the sheet.
I assigned each macro to the button in my Excel sheet. They are not command buttons with code.

When I enter the code you gave it says Invalid outside procedure

Gosh thank you for the assistance.

Would it help if I give you my whole Excel
 
Upvote 0
Yes yes that is how I created them, and by names do you mean the text?
 
Upvote 0
by names do you mean the text?

No
- buttons have names
- right click on each button and look in the name box (above cell A1)
- if you have not renamed the buttons then the names will probably be
Button 1
Button 2
Button 3

The correct term for the text on the button is Caption
What is the caption on each button?
 
Upvote 0
Okay I corrected them, thank you so much. It still gives the same error tho... Lol sorry I am still new
 
Upvote 0
The code must be amended because this method only applies to active-x objects
VBA Code:
btnPause.Enabled = True


"Okay I corrected them"
[/QUOTE]
What have you done?
Have you renamed the buttons like this ...
btnPause
btnStop
btnReset

Let me know and I will rewrite the code for you later today
 
Upvote 0
So I have renamed all the buttons as you said in the filed above Cell A1, and then I add the new amended code you so kindly suggested.
But it still seems to not work. Gosh I spent the whole day yesterday and it just isn't working lol.
I don't even know id the time will show or not. I think the whole thing is just incorrect heehehehe.

I did write a successful timer stop/reset/start module with another Excel. But it did not have a pause in. Every time the start button is clicked it restarted the timer.

My end goal is to start the timer, pause it when I am done with that task and stop it when I am completely finalised. Then reset to start it over.
This Excel sheet will be my timesheet for everything I do at work. But I just can't seem to make the pause work.

I will be creating round about 50 buttons in the end. To record the different sections of my work in real-time.


If you could assist me in completing this code I would really really be so grateful.
 
Upvote 0
[
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.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


][/CODE]


My code is like that now, and the only other thing is the 4 buttons which I renamed like you said and the captions all say Pause, Stop, Start and Reset.

No other modules exist and no other sheets except Sheet1 in Excel
 
Upvote 0

Forum statistics

Threads
1,212,928
Messages
6,110,737
Members
448,295
Latest member
Uzair Tahir Khan

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