stopwatch - pause button

monkeymafia

New Member
Joined
Sep 22, 2009
Messages
2
Hi all,

I have a simple stopwatch program which i've created in excel, with a start, stop and save button. does anybody know how I would go about making a pause button work. So when the pause button is pressed the timer can be resumed by pressing the start button.

This is my code:
Code:
Dim CmdStop As Boolean
Dim PauseTime, Start, Finish, TotalTime

Sub Btn1_Click()
CmdStop = 0
If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
    PauseTime = 5    ' Set duration.
    Start = Timer    ' Set start time.
    Do While CmdStop = 0
        TimerValue = Timer - Start
        DoEvents    ' Yield to other processes.
    Loop
    Finish = Timer    ' Set end time.
    TotalTime = Finish - Start    ' Calculate total time.
    MsgBox "Paused for " & TotalTime & " seconds"
Else
    End
End If
End Sub

Private Sub btnPause_Click()
???
End Sub

Private Sub BtnReset_Click()
TimerValue = 0
End Sub

Sub BtnStop_Click()
CmdStop = 1
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Here is a modified version of your code featuring simple Start, Pause/Continue, Stop and Reset capability:

Code:
Option Explicit
Dim CmdStop As Boolean
Dim Paused As Boolean
Private Sub btnStart_Click()
    CmdStop = False
    Paused = False
    Dim TimerValue As Date
    Dim pausedTime As Date
    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
Private Sub btnPause_Click()
    If btnPause.Caption = "Pause" Then
        Paused = True
        btnPause.Caption = "Continue"
    Else
        Paused = False
        btnPause.Caption = "Pause"
    End If
 
End Sub
Private 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

It should be straightforward to adapt it to your requirements.

Hope this helped,
Rolf Jaeger
SoarentComputing
http://soarentcomputing.com

Phone: 800.580.0068
Cell: 510.300.7462
 
Upvote 0

Forum statistics

Threads
1,213,489
Messages
6,113,954
Members
448,535
Latest member
alrossman

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