While working at IBM, I was a part of a ToastMasters International club. It's a place where you can improve your communication and leadership skills.
An interesting section in the weekly meetings was impromptu speaking. In this part of the meeting, a person was given a topic and he/she had to speak on it for 2 minutes. There was a designated person who used to time the speech and shows a green card at 1 minute, a yellow card at 1:30 minutes, and a red card after 2 minutes.
Usually, a cell phone or a watch is used to time the speech and the time taken is recorded manually on a paper. It often happens that the person forgets to show the colored cards or sometimes forgets to record the timing for the speakers (happened with me all the time). With these things in mind, I have created a stopwatch in Excel that would help time and record the speeches.
Let's first learn how to create a basic stopwatch in Excel.
How to Create a Stopwatch in Excel (Basic)
By 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.
Something as shown below:
To create this stopwatch in Excel, you need to know about the Application.Ontime method in VBA.
Application.OnTime Method in VBA
Application.OnTime method can be used when you want to run a specified code in future. 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.
Syntax of Application.OnTime Method:
Application.OnTime(EarliestTime, Procedure, LatestTime, Schedule)
- EarliestTime: The time when you want to run the procedure.
- Procedure: The name of the procedure that should be run.
- 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. For example, it could be EarliestTime + 45 (which means it will wait for 45 seconds for the other procedure to get completed). If even after 45 seconds the procedure it not able to run, it gets abandoned. If you don't specify this, Excel would wait until the code can be run, and then run it.
- Schedule (Optional): If set to True, it schedules new time procedure. If False, then it cancels the previously set procedure. By default, this is True.
An Example of Application.OnTime Method
Sub test() Application.OnTime Now + TimeValue("00:00:05"), "ShowMessage" End Sub Sub ShowMessage() MsgBox ("HELLO") End Sub
The first part of the macro uses the Application.OnTime method and runs the procedure ShowMessage (in double quotes) after five seconds. The ShowMessage procedure simply shows the message HELLO.
You can use this format to run any procedure after a specified time from the current time.
Now using this concept, let's look at the code for creating a simple stopwatch in Excel.
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
This code has 3 parts:
- The first part initializes the current time to the variable t. Then it calls another procedure StartTimer.
- StartTimer procedure uses a variable NextTick which gets incremented by 1 with every passing second. In the worksheet, cell A1 has the running timer as we have specified NextTick – t -TimeValue(“00:00:01”), “hh:mm:ss”) to be the value in that cell. It then runs the StartTimer code again after every second.
- StopTimer cancels the Application.Ontime procedure by making the schedule value False. This stops the timer.
Here is what you'll get with the above code (I have assigned the macros to the start/stop buttons):
This is a basic stopwatch in Excel.
I call it basic as you can not stop in middle and restart where you left. It will always restart from 1 when you press the start button.
Now that you have learned the basics of Application.OnTime method, you can easily tweak this to create whatever kind of stopwatch you want in Excel.
Stopwatch in Excel (For ToastMasters)
I have used the concept discussed above and created a Stopwatch in Excel that can be used in the Toastmasters meeting (which I mentioned at the beginning of this tutorial).
Here are the things that can be done using this stopwatch:
- You can stop the timer and then restart again from the same time (recorded till then).
- You can reset the timer. This sets the timer value to 0. As soon as you do that it automatically records the total time elapsed.
- It changes the color of the timer box, depending on stopwatch value (this could be a good reminder to show the green/yellow/red cards).
Here is how it looks:
In this above demo, I have set the color change after every five seconds. You can easily specify when you want the color to change (green card at 1 min, yellow card at 1.5 minutes, and red card at 2 minutes) by changing the values in the Calculation sheet.
As soon as you hit the reset button, the color of the timer would go back to white, the value of the timer would become 0, and it will record the time in column G.
Note: Since these files contain macro, you will have to enable macros before using it. When you open the workbook, you'll see a yellow button – Enable Content. Click on it to enable macros.
If you create something cool using a timer, do share it with me.
You May Also Like the Following Excel Tutorials:
- Matrix Falling Numbers Effect in Excel using VBA.
- How to Automatically Insert Date and Time Stamp in Excel.
- Using Loops in Excel VBA.
- Free Excel Templates.
- Creating a Dashboard in Excel.
30 Ready To Use Excel Macro Examples
22,000+ Excel enthusiasts have already downloaded