Does anyone know how to make a timer / countdown function that runs for 2 minutes and then switches from one sheet to another?
So:
Run timer
If time = 120 sec
Then go to sheet "start"
Thank you
Does anyone know how to make a timer / countdown function that runs for 2 minutes and then switches from one sheet to another?
So:
Run timer
If time = 120 sec
Then go to sheet "start"
Thank you
You'd use Application.Ontime
Something like below...
See link for more detailed tutorial on Application.OnTime.![]()
Sub RunTwoMin() Dim TimeToRun As Date TimeToRun = Now + TimeValue("00:02:00") Application.OnTime TimeToRun, "selectStart" End Sub Sub selectStart() Worksheets("start").Select End Sub
http://www.snb-vba.eu/VBA_Application.OnTime_en.html
Hi Andre,
I don't know how you want to trigger the code, but running start_timer will wait 2 minutes, then worksheet "Start" will activate.
![]()
Sub start_timer() Application.OnTime Now + TimeValue("00:02:00"), "goto_sheet" End Sub Sub goto_sheet() Worksheets("Start").Activate End Sub
Please help by:
Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know
There are 10 kinds of people in this world... those who understand binary, and those who don't.
Sir askadi how to do use doevents not application on time.. I ever see use doevents how to fix this code
[/QUOTE]![]()
Sub start_timer() Dim t t =timer Do Doevents If format(round(t - timer,2),"00:00:00.00")="02.00" then Sheets("start").activate Exit do End if Loop End Sub
Last edited by daboho; 01-30-2017 at 05:20 PM.
DoEvents just allows the code to delay to run other things (I don't find it helps a lot most of the time), however, the method you are looking for should be something like this... round, 2 gives seconds which is why I've made it stop at 120. I put >= in case it is 121 seconds for some reason... if we stop only if =120 then if we miss 120 exactly it will never stop:
![]()
Sub start_timer() Dim t t = Timer Do DoEvents If Round(Timer - t, 2) >= 120 Then Sheets("start").Activate Exit Do End If Loop End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks