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: 263
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.

This may only be the beginning of your timer-related problems which is why I provided suggestions in post#12 and post#19

What is being recorded ?
Where is it being recorded ?
What is the name of the "correct" workbook

Please supply the code that is causing your problem
Click on <vba/>
and ..
[ CODE=vba ] paste code here[ /CODE ]
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
VBA Code:
Option Explicit
Public sTimer As Boolean
Sub Start_Timer()
        Application.OnTime Now + TimeValue("00:00:01"), "IncreamentTimer"
End Sub

Sub IncreamentTimer()
        If Range("K9").Value = "" Then Range("K9").Value = TimeValue(Now)
        If Range("K8").Value = "" Then Range("K8").Value = TimeValue(Now)
        Range("K8").Value = Range("K8").Value + TimeValue("00:00:01")
        If Range("J8").Value = "" Then Range("J8").Value = "Timer ON"
        Range("J9").Value = "Timer Start Time"
        Range("J8").Font.Color = vbGreen
        sTimer = True
        Start_Timer
End Sub

Sub PauseResume_Timer()
        If Range("K9").Value <> "" Then
                If sTimer = True Then
                        sTimer = False
                        Application.OnTime Now + TimeValue("00:00:01"), "IncreamentTimer", schedule:=False
                        Range("J8").Value = "Timer Paused"
                        Range("J8").Font.Color = vbRed
                Else
                        Application.OnTime Now + TimeValue("00:00:01"), "IncreamentTimer"
                        Range("J8").Value = "Timer Resumed"
                        Range("J8").Font.Color = vbGreen
                        
                End If
        Else
                MsgBox "You can only click Pause/Resume button when Timer is On.", vbExclamation, "Timer Off!"
        End If
End Sub

Sub Stop_Timer()
        On Error Resume Next
        Application.OnTime Now + TimeValue("00:00:01"), "IncreamentTimer", schedule:=False
        If Err = 0 Then
                Range("L9").Value = Format(Range("K8").Value - Range("K9").Value, "hh:mm:ss")
                Range("J8").Value = "Timer OFF"
                Range("J8").Font.Color = vbBlack
        Else
                MsgBox "Timer is currently OFF.", vbExclamation, "Timer is OFF!"
        End If
End Sub

Sub Reset_Timer()
        Range("K8").Value = ""
        Range("K9").Value = ""
        Range("J8").Value = ""
        Range("J9").Value = ""
        Range("L9").Value = ""
End Sub
 
Upvote 0
So I attached a picture of my excel workbooks side by side. The left workbook is where everything happens. As soon as you click on the right workbook the macro starts recording in that workbook
 

Attachments

  • Untitled.png
    Untitled.png
    163 KB · Views: 33
Upvote 0
The coding is in a standard module called Module 1. I attached my picture
 

Attachments

  • Untitled 2.png
    Untitled 2.png
    109 KB · Views: 18
Upvote 0
Explanation
Your code is in a module
Your ranges are unqualified
Unqualified ranges
in a module refer by default to the ActiveSheet
The active sheet is the sheet currently active in the currently active workbook
That is why the entries are being written to the incorrect workbook

To qualify a range
Range requires requires both a sheet and workbook reference
VBA Code:
ThisWorkbook.Sheets("Name of Sheet").Range("A1:A10") = "xxx"

If referring to the worksheet repeately, it makes sense to declare and then use a variable.
So code below does the same, but ws can be used repeatedly in the same procedure (as illustrated for cell A1)
VBA Code:
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Name of Sheet")
ws.Range("A1:A10") = "xxx"
ws.Range("A1")="y"

Before amending anything ...
What should happen when a different workbook is active?
Should values be recorded?
 
Upvote 0
Thank you so much, and no this workbook should just record the time. Nothing should happen in any other workbooks. This timer workbooks should be completely
independant.
 
Upvote 0
IF the timer is required to stop/pause (when the workbook is deactivated) ... and restart again automatically (when the workbook is re-activated)

THEN ... 2 specific event macros are required and they MUST be placed in ThisWorbook code area (not a module)

VBA Code:
Private Sub Workbook_Activate()
'command to start or re-start the timer goes here
End Sub

Private Sub Workbook_Deactivate()
'command to pause or stop the timer goes here
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,606
Messages
6,125,800
Members
449,261
Latest member
Rachel812321

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