Using the VBA Timer
to Create a Stopwatch
This tutorial uses the VBA timer to create a stopwatch on a userform. You can use either Excel or Word.
First, go to the VBA Editor/IDE; ALT + F11.
Right click on any line/branch in the VBAProject Explorer at the upper left below the toolbars. Go to Insert and click on UserForm.
Click on the capital A, which is the VBA Label control, and then drag the mouse on the form in a down and to the right fashion to create a label. You can resize the label after you have draw/created it by using the white squares along the border of the label.
SIDEBAR----- you can also resize the form by clicking on any blank space on it and using the white squares along the border.
Next, click on the commandbutton and create three of them along the bottom of the form.
Next, using the Properties window, rename the captions from their defaults to Start, Stop, and Reset and the button names to btnStart, btnStop, btnReset. When you click on a control, here a button, you can change its properties.
Now in the Project pane on the left, right click on the userform and choose Show Code to open the code window. Paste the following code into the code window...
Dim dteStart As Date, dteFinish As Date
Dim dteStopped As Date, dteElapsed As Date
Dim boolStopPressed As Boolean, boolResetPressed As Boolean
Private Sub btnReset_Click()
dteStopped = 0
dteStart = 0
dteElapsed = 0
Label1 = "00:00:00"
boolResetPressed = True
Private Sub btnStart_Click()
dteStart = Time
boolStopPressed = False
boolResetPressed = False
dteFinish = Time
dteElapsed = dteFinish - dteStart + dteStopped
If Not boolStopPressed = True Then
Label1 = dteElapsed
If boolResetPressed = True Then GoTo Start_timer
Private Sub btnStop_Click()
boolStopPressed = True
dteStopped = dteElapsed
Private Sub UserForm_Initialize()
Label1 = "00:00:00"
Please note that this VBA timer is only exact to the second and not anything less than.
This site is powered by Site Build It!. If you enjoy it, please check out the
Build It homepage to learn more and on how to build
a success-guaranteed site with no technical skills.
Go to the next tutorial: Comming Soon
Return from VBA Timer to Free VBA Tutorials