![]() ![]() ![]() Then it calls another procedure StartTimer. The first part initializes the current time to the variable t.Dim NextTick As Date, t As Date Sub StartStopWatch() t = Time Call StartTimer End Sub Sub StartTimer() NextTick = Time + TimeValue("00:00:01") Range("A1").Value = Format(NextTick - t - TimeValue("00:00:01"), "hh:mm:ss") Application.OnTime NextTick, "StartTimer" End Sub Sub StopTimer() On Error Resume Next Application.OnTime EarliestTime:=NextTick, Procedure:="StartTimer", Schedule:=False End Sub Now using this concept, let’s look at the code for creating a simple stopwatch in Excel. You can use this format to run any procedure after a specified time from the current time. The ShowMessage procedure simply shows the message box with the prompt HELLO. The first part of the macro uses the Application.OnTime method and runs the procedure ShowMessage (in double quotes) after five seconds. By default, this is True.Īn Example of Application.OnTime Method Sub test() Application.OnTime Now + TimeValue("00:00:05"), "ShowMessage" End Sub Sub ShowMessage() MsgBox ("HELLO") End Sub If False, then it cancels the previously set procedure. Schedule (Optional): If set to True, it schedules new time procedure.If you don’t specify this, Excel would wait until the code can be run, and then run it. If even after 45 seconds the procedure it not able to run, it gets abandoned. For example, it could be EarliestTime + 45 (which means it will wait for 45 seconds for the other procedure to get completed). LatestTime (Optional): In case another code is running and your specified code can’t be run at the specified time, you can specify the LatestTime for which it should wait.Procedure: The name of the procedure that should be run.EarliestTime: The time when you want to run the procedure.OnTime( EarliestTime, Procedure, LatestTime, Schedule) Syntax of Application.OnTime Method:Īpplication. ![]() For example, you may use it to show a message box to remind you to get up and stretch your legs after 1 hour or have medicines after 3 hours. Application.OnTime Method in VBAĪpplication.OnTime method can be used when you want to run a specified code in the future. To create this stopwatch in Excel, you need to know about the Application. How to Create a Stopwatch in Excel (Basic)īy a simple/basic stopwatch in Excel, I mean something that would start when we press the start button and stop when we press the stop button. How to Create a Stopwatch in Excel (Basic). ![]()
0 Comments
Leave a Reply. |