Using the VBA Timerto Create a StopwatchThis 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. SIDEBAR----- 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 End Sub Private Sub btnStart_Click() Start_timer: dteStart = Time boolStopPressed = False boolResetPressed = False Timer_Loop: DoEvents dteFinish = Time dteElapsed = dteFinish - dteStart + dteStopped If Not boolStopPressed = True Then Label1 = dteElapsed If boolResetPressed = True Then GoTo Start_timer GoTo Timer_Loop Else Exit Sub End If End Sub Private Sub btnStop_Click() boolStopPressed = True dteStopped = dteElapsed End Sub Private Sub UserForm_Initialize() Label1 = "00:00:00" End Sub 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 Site
Build It homepage to learn more and on how to build
a success-guaranteed site with no technical skills. Custom
Search
Go to the next tutorial: Comming Soon Return from VBA Timer to Free VBA Tutorials |