Stopwatch

stuartchaffey

New Member
Joined
Dec 28, 2021
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi,
May seem a simple question but I have spent hours googling and cant work it out, in a userform I have 2 command buttons and 1 text box. when I press commandbutton4 I want textbox5 to start counting up in seconds. When I press commandbutton5 I want the timer to stop.
Any help would be appreciated
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Welcome to the board.
In a standard module put these three sub routines and the two public declarations. :
VBA Code:
Public ttimer
Public TimeFlag
Sub starttim()
TimeFlag = False
ttimer = Now()
Call runtimer
End Sub

Sub runtimer()
If TimeFlag = True Then
  MsgBox ("timer stopped at " & ttimer)
Else
 Displayt = Now() - ttimer
 ActiveSheet.TextBox1.Text = Format(Displayt, "Long Time")
 Application.OnTime (Now() + TimeValue("00:00:01")), "runtimer"
End If
 
End Sub

Sub stoptim()
TimeFlag = True
End Sub
Then put this code in your two command buttons:
VBA Code:
Private Sub CommandButton1_Click()
Call starttim
End Sub

Private Sub CommandButton2_Click()
Call stoptim
End Sub
I have assume that your textbox is called textbox1 and is on the active sheet. If any of these assumptions are wrong just change the names as appropriate.
The time increments once a second.
 
Upvote 0
Hey,
Thankyou for the reply, but its not showing the time in the text box on the userform. Any ideas? userform is called crystalitesTEAMANDLEADER and its text box 5 on the userform where i want it to display time elapsed. Again thank you
 
Upvote 0
The problem is getting the correct name for the textbox, try changing :
VBA Code:
ActiveSheet.TextBox1.Text = Format(Displayt, "Long Time")
to
VBA Code:
crystalites.TEAMANDLEADER.Text = Format(Displayt, "Long Time")
if not it is going to be something like that try googling adddressing a textbox on a userform in vba
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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