Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
im trying to create a timer in excel using vba but im not getting any where...
im looking for a timer that will be on the worksheet that has buttons to start stop pause and reset. the timer i want to show in a text box on the worksheet...also when the workbook is closed i need the value of where the timer is at to be saved that way when i can pick up where i left off...
i already have the buttons and text box on my worksheet...i just lacking the code to make it work...can anyone help me please?

Here is how ti mplement a timer in Ecxel
create a button on sheet1 and paste the following code in it click event
Private Sub CommandButton1_Click()
StartTimer
End Sub
Now insert a modual and copy paste the following into it.Public RunWhen As Double
Public Const cRunIntervalSeconds = 1
Public Const cRunWhat = "The_Sub"Sub StartTimer()
Application.ScreenUpdating = False
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
schedule:=True
End SubSub The_Sub()
Sheet1.Range("A1").Value = Sheet1.Range("A1").Value + 1
StartTimer
End Sub

ok great that will work but now how can i stop it and pick back up where it left off? it also needs to count min and sec not just sec...
i added this to the sheet
Private Sub cmdReset_Click()
Sheet3.Timer.Caption = 0
End Subthis works for my reset i just need a stop...

ok all i have a start stop and reset... thank you Always...i modified your code slightly...heres what i have:
in my Module:
Public RunWhen As Double
Public Const cRunIntervalSeconds = 1
Public Const cRunWhat = "The_Sub"Sub StartTimer()
Application.ScreenUpdating = False
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
schedule:=True
End SubSub The_Sub()
Sheet3.Timer.Caption = Sheet3.Timer.Caption + 1
If Sheet1.cmdStart.Enabled = False Then _
StartTimer
End Subon my sheet1:
Private Sub cmdReset_Click()
Timer.Caption = 0
cmdStart.Enabled = True
cmdStop.Enabled = False
cmdReset.Enabled = True
End SubPrivate Sub cmdStart_Click()
cmdStart.Enabled = False
cmdStop.Enabled = True
cmdReset.Enabled = False
StartTimer
End SubPrivate Sub cmdStop_Click()
cmdStart.Enabled = True
cmdStop.Enabled = False
cmdReset.Enabled = True
End Sub
and this all works..the only problem is is that it is in seconds...how can i get min and secs?

you can use an if statement to say if sheet3.timer.caption <= 60 then "A3" .value= "A3".value +1 that will then capture the minutes. then the seconds will have to reset to 0 again. Ill try to have a go at it and send a peice of code back to you.

![]() |
scheduling shifts
|
Best Office Suite
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |