Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Excel timer

0

Hi all, 

I got a question about countdown timer. i want to set it for countup timer. Will it work if if i rework ur code like this? See code below. 

Also i need to set stop watch for about 20/30 rows in excel. At some point i need to have up to 5 working same time is that possible?

Any help will be much appericiated. 


Thank You 

Public interval As Date
Sub timer()
 
    interval = Now + TimeValue("00:00:01")

    Range("A1") = Range("A1") + TimeValue("00:00:01")
    
    Application.OnTime interval, "timer"
     
End Sub
Sub stop_timer()
 
    Application.OnTime EarliestTime:=interval, Procedure:="timer", Schedule:=False
 
End Sub
Answer
Discuss

Answers

0
Selected Answer

Hi Narutoszym and welcome to the Forum

Yes, the timer will countup by adding the seconds (TimeValue), as per your code.

I think you can have 5 timers running simultaneously (at least on my old PC!) but the VBA code needs to have separate condes and varaible names to avoid ambiguity.

In the attached file, there are countup timers in cells A1, A5, A9, A13 and 17 and buttons to start, stop and reset each timer. You can start them independently from where they have reached (as the file leaves me) or reset and restart.

The duplicated code (including the reset macro is shown below with changes in bold for timer 2 only but you'll get the idea:

Public interval As Date, interval2 As Date, interval3 As Date, interval4 As Date, interval5 As Date

Sub timer()

    interval = Now + TimeValue("00:00:01")

    Range("A1") = Range("A1") + TimeValue("00:00:01")

    Application.OnTime interval, "timer"

End Sub

Sub stop_timer()
    On Error Resume Next
    Application.OnTime EarliestTime:=interval, Procedure:="timer", Schedule:=False

End Sub

Sub reset_timer()
Range("A1") = 0
End Sub

Sub timer2()

    interval2 = Now + TimeValue("00:00:01")

    Range("A5") = Range("A5") + TimeValue("00:00:01")

    Application.OnTime interval2, "timer2"

End Sub

Sub stop_timer2()
    On Error Resume Next
    Application.OnTime EarliestTime:=interval2, Procedure:="timer2", Schedule:=False

End Sub

Sub reset_timer2()
Range("A5") = 0
End Sub

Sub timer3()

    interval3 = Now + TimeValue("00:00:01")

    Range("A9") = Range("A9") + TimeValue("00:00:01")

    Application.OnTime interval3, "timer3"

End Sub

Sub stop_timer3()
    On Error Resume Next
    Application.OnTime EarliestTime:=interval3, Procedure:="timer3", Schedule:=False

End Sub

Sub reset_timer3()
Range("A9") = 0
End Sub

Sub timer4()

    interval4 = Now + TimeValue("00:00:01")

    Range("A13") = Range("A13") + TimeValue("00:00:01")

    Application.OnTime interval4, "timer4"

End Sub

Sub stop_timer4()
    On Error Resume Next
    Application.OnTime EarliestTime:=interval4, Procedure:="timer4", Schedule:=False

End Sub

Sub reset_timer4()
Range("A13") = 0
End Sub

Sub timer5()

    interval5 = Now + TimeValue("00:00:01")

    Range("A17") = Range("A17") + TimeValue("00:00:01")

    Application.OnTime interval5, "timer5"

End Sub

Sub stop_timer5()
    On Error Resume Next
    Application.OnTime EarliestTime:=interval5, Procedure:="timer5", Schedule:=False

End Sub

Sub reset_timer5()
Range("A17") = 0
End Sub
Note that I added the line On Error Resume Next in the stop macros (to avoid problems if the stop macro is run without the timer running.

Hope this helps

Discuss

Discussion

Do You know that You are a star? Works perfect. Thank You!
narutoszym (rep: 2) Dec 6, '21 at 12:46 pm
Glad it worked for you
John_Ru (rep: 6142) Dec 6, '21 at 12:49 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login