Is this possible at all. I may need to add 2 more buttons called Pause Timer and Restart Timer. The Stop Button will only be used at the end of the day. We need to be able to Pause the loop timer and restart it again when people are at lunch or tea break.
There is not really a need for two more buttons, because the stop button can reset our internal timer and we can use this as indicator how the start button should act.
If the start button is clicked, the timer is initialized and starts, it shows the caption "Pause".
If clicked again, the timer pauses and the button shows "Continue".
If clicked again, the timer continues and the button shows "Pause".
The button toggles in this behavior until you click the stop button.
If the stop button is clicked, the start button shows the caption "Start", stops the timer, reset cell to zero, as usual.
Don't worry if you forget to click the stop button at the end of the day, click it the next morning and everything is okay.
To increase a counter when the timer resets is very easy, have a look at the sub Initialize().
I made a little change with the cell who holds the TimerValue, you must setup this cell with a date/time format and enter the time in this format, i.E.: 0:10:00 for a 10 minute countdown. This allows you to have a more flexible counter, it counts now any
value from 1 second up to several hours (and more).
I've added some comments and renamed the timersub, so it is easier for your to read and understand what happen in the macros, I hope. :-)
Place this code in the code module of the sheet:
--- schnipp ---
Option Explicit
Private Captions(-1 To 1) As String
Private Sub Initialize()
'We need to init only if this cell is Nothing
If TimerCell Is Nothing Then
'Setup the cell where the timer is shown
Set TimerCell = Range("A2")
'Setup the cell where the timer get the countdown value
Set TimerValue = Range("B2")
'Setup the cell to show the counts
Set TimerCounter = Range("C2")
'Setup the altering captions of the first commandbutton
Captions(1) = "Start"
Captions(False) = "Pause"
Captions(True) = "Continue"
End If
End Sub
Private Sub CommandButton1_Click()
'Starts, pauses or continues the timer
'Make sure our cells are initialized
Initialize
'How should we act?
If EndTime = 0 Then
'Keep timer running
StopTimer = False
'Call our timer
ShowTimer
Else
'Toggle the timer state
StopTimer = Not StopTimer
'Did we have to continue the timer?
If Not StopTimer Then
'Calculate new endtime
EndTime = Now + TimerCell
'Call our timer
ShowTimer
End If
End If
'Set caption to next action
CommandButton1.Caption = Captions(StopTimer)
End Sub
Private Sub CommandButton2_Click()
'Stops the timer
'Make sure our cells are initialized
Initialize
'Force timer to stop
StopTimer = True
'Reset cell to zero
TimerCell = 0
'Reset our internal time
EndTime = 0
'Force the caption of the first button
CommandButton1.Caption = Captions(1)
End Sub
--- schnapp ---
And this code in the normal code module:
--- schnipp ---
Option Explicit
'Avoids that the user can call our sub directly
Option Private Module
Public TimerCell As Range
Public TimerValue As Range
Public TimerCounter As Range
Public StopTimer As Boolean
Public EndTime As Double
Public Sub ShowTimer()
'Timer running?
If Not StopTimer Then
'To avoid an event every second!
Application.EnableEvents = False
'Time over?
If EndTime - Now < 0 Then
'Increase counted value only after first run
If EndTime > 0 Then TimerCounter = TimerCounter + 1
'Calculate next endtime
EndTime = Now + TimerValue
End If
'Show remaining time
TimerCell = EndTime - Now
'Enable the events
Application.EnableEvents = True
'Force excel to call ourself in one second
Application.OnTime Now + TimeSerial(0, 0, 1), "ShowTimer"
End If
End Sub
--- schnapp ---
I hope this come across with your imagining.
Andreas.