How to Create a Count up & Countdown Timer in PowerPoint using VBA
❎ No manual animations! We are going to code the timer in PowerPoint!
Sub countdown() Dim time As Date time = Now() Dim count As Integer count = 30 'assuming 30 seconds time = DateAdd("s", count, time) Do Until time < Now() DoEvents ActivePresentation.SlideShowWindow.View.Slide.Shapes("countdown").TextFrame.TextRange = Format((time - Now()), "hh:mm:ss") Loop End Sub
We need to run this VBA Macro when we click on this shape in Slide Show Mode. To do this, select the shape, go to Insert | Actions | Run Macro | countdown
Now, go to Slide Show | From Beginning and click the shape, you will see that the code is triggered and your countdown timer starts running! You can also now customise the shape’s colours and fonts.
How to create a Countdown Timer in PowerPoint?
The following video tutorial goes into the details of the VBA Code of the Countdown Timer. It also shows how we can have the Countdown Timer span across multiple slides of the presentation. We can also trigger an action to occur when the time is up.
The possibilities are endless with VBA in PowerPoint to customise your countdown timer. You can even make a count-up timer! Following are a few popular requests and their corresponding VBA Codes.
VBA Code for Countdown Timer
Dim time As Date time = Now() Dim count As Integer count = 30 'assuming 30 seconds time = DateAdd("s", count, time) 'adding 30 seconds
Now() refers to the current date and time. It is stored in the variable called
time. We then add 30 seconds to
time. Hence, we shall refer to
time as the future time.
Let us also understand more about the items present within the
"s"→ the unit of what we are adding.
count→ how much should be added.
time→ to what it should be added.
Line 6 of the code translates to ‘add 30 seconds to
time‘, if you wanted to add 30 minutes instead of 30 seconds, we would have to change
To provide an example, if the time currently stored is
00:00:00, it changes to
00:00:30 once 30 seconds is added to it. Hence, the time stored is 30 seconds into the future.
Do Until time < Now() Loop
A conditional loop is present to update the text within the countdown shape. The condition is that the loop must continue until
Now() becomes greater than
time. To continue the example, as the current time ticks from
00:00:30 the loop occurs, however, once it is
00:00:31, the loop stops as the current time has become greater than our set future time.
To summarise, we wait for the current time to catch up with the future time, and until that happens, we update the countdown shape with the difference between the future time and the current time.
ActivePresentation.SlideShowWindow.View.Slide.Shapes("countdown").TextFrame.TextRange = Format((time - Now()), "hh:mm:ss")
Customising Countdown Timer Format
The following are the syntax for various formats of displaying time in VBA. If you want more detailed information about the Format Function, you can refer to the documentation.
Use the format
"nn:ss" if you want the minutes and seconds. In case you do not want a leading zero for minutes, you can use
"mm:ss" would not work. Generally.
m stands for months and
n stands for minutes. However,
m is referred to as minutes only when it is present after
Use the format
"ss" if you want only the seconds to be visible.
Use the format
"hh:mm:ss" if you want the hours, minutes and seconds to be visible.
Use the format
"yy:mm:ww:dd" in your PowerPoint Countdown Timer. This would be more suited if you had to countdown to a particular date and time in the future.
Notification on Completion of Countdown
Once the current time surpasses the future time, we can trigger a
MsgBox pop-up to notify us that the countdown is over. This is possible with an
if-then condition present within the
Do Loop. Instead of a message box, you can also redirect the presentation to a certain slide or play a sound effect.
If time < Now() Then 'add your code here MsgBox "Time Up!" End If
Change Countdown Value in Slide Show Mode
If you want to change the countdown value directly in Slide Show Mode without touching the VBA Code, we can add an ActiveX Element Textbox named
TextBox1 in our slide. We can type the number of seconds we would want the countdown to occur within it. This input is going to be the value of the variable
count. We can read the input using the following code:
count = ActivePresentation.Slides(1).Shapes("TextBox1").OLEFormat.Object.Value
If you’re creating a PowerPoint Template and want the user to input a custom time, we can take the text of a particular shape and make that the value of
count. We can also place the shape outside the slide or on a separate slide. Let us name this shape
count = ActivePresentation.Slides(1).Shapes("TimeLimit").TextFrame.TextRange
Countdown Timer for a Specific Date or Time
Sub countdown() Dim time As Date 'change the date and time within the brackets 'currently it is 13 October 2022, 3 AM time = DateSerial(2022, 10, 13) + TimeSerial(3, 0, 0) Do Until time < Now() DoEvents ActivePresentation.Slides(1).Shapes("countdown").TextFrame.TextRange = DateDiff("d", Now(), time) & " Days " & Format((time - Now()), "hh:mm:ss") Loop End Sub
Timer Across Multiple PowerPoint Slides
In order to embed the same countdown timer throughout multiple PowerPoint Slides: if there is a timer for 30 seconds and we go to the next slide after 10 seconds, the timer in the slide should resume the countdown from 20.
To accomplish this, we would need to add a
For Loop. All the slides in the range of
i (in this case: 1 to 5) will be updated until the current time passes the future time.
For i = 1 To 5 ActivePresentation.Slides(i).Shapes("countdown").TextFrame.TextRange = Format((time - Now()), "ss") Next i
Increase Countdown Timer by Adding Additional Time in Slide Show Mode
We can also increase or decrease the countdown timer while in PowerPoint Slide Show Mode. This feature is commonly used by teachers playing PowerPoint Games in their classroom. For example, while playing a timed quiz game, the time limit can be decreased on click of a wrong answer. Similarly, the countdown timer can also be increased.
Initially, we need to declare the variable
time above all the sub-routines. This will allow us to reference the same variable within various other sub-routines:
SubtractTime. Since we are declaring it once, we do not have to declare it once more within the sub-routines.
Global time As Date Sub countdown() time = Now() time = DateAdd("s", 30, time) 'assuming 30 seconds Do Until time < Now() DoEvents ActivePresentation.SlideShowWindow.View.Slide.Shapes("countdown").TextFrame.TextRange = Format((time - Now()), "hh:mm:ss") Loop End Sub
We can modify the
time variable using the
DateAdd function. The following sub-routines will increase or decrease the countdown timer. The macro can be run on click of another shape.
Sub AddTime() time = DateAdd("s", 10, time) 'increase timer by 10 seconds End Sub
Sub SubtractTime() time = DateAdd("s", -10, time) 'decrease timer by 10 seconds End Sub
Show only Seconds in Countdown Timer
If you have a countdown timer for 2 minutes, it would show as
00:00. However, if you edit the code such that only seconds are shown by changing the format to
"ss", you would notice that the countdown timer starts with
60 and ends at
00 and repeats once more! That is because the
"ss" format can’t show above
DateDifffunction. Now, the countdown timer will start from
120and end at
Sub countdown() Dim time As Date time = Now() time = DateAdd("s", 120, time) Do Until time < Now DoEvents ActivePresentation.SlideShowWindow.View.Slide.Shapes("countdown").TextFrame.TextRange = DateDiff("s", Now(), time) Loop End Sub
Pause & Resume PowerPoint Timer
You can pause and resume your PowerPoint Countdown Timer using the following VBA Code. Have 3 shapes on your slide, on click of which the following macros would be run:
- Start Button →
- Pause Button →
- Resume Button →
When the Pause Button is clicked, the timer freezes and the remaining time is calculated using the
DateDiff Function. When the countdown timer is resumed, the future time is updated by adding the remaining time to
Dim time As Date 'the future time for the countdown Dim pausedTime As Date 'time at which countdown is paused Dim count As Integer 'countdown value Dim PauseT As Boolean 'is the timer paused? Sub PlayCountdown() PauseT = False time = Now() count = 300 '5 minute timer time = DateAdd("s", count, time) Debug.Print time countdown End Sub Sub PauseCountdown() PauseT = True pausedTime = time - Now() count = DateDiff("s", 0, pausedTime) End Sub Sub ResumeCountdown() time = DateAdd("s", count, Now()) countdown End Sub Sub countdown() PauseT = False Do Until time < Now() DoEvents If PauseT = False Then ActivePresentation.SlideShowWindow.View.Slide.Shapes("countdown").TextFrame.TextRange = Format((time - Now()), "hh:mm:ss") Loop End Sub
VBA Code for Count Up Timer
Similarly, we can also make a Count Up Timer in PowerPoint using VBA Codes. In this case, we have three different parts:
time1stores the time of when the macro is run
time2stores the future time of when the count up should be over
Now()is a dynamic function. It always shows the current time.
To give an example, if I run the following VBA Code of the 30 Seconds Count Up Timer at midnight, which is
time1 would be
time2 would be
Sub countup() Dim time1 As Date, time2 as Date time1 = Now() time2 = Now() Dim count As Integer count = 30 'assuming 30 seconds' time2 = DateAdd("s", count, time2) Do Until time2 < Now() DoEvents ActivePresentation.SlideShowWindow.View.Slide.Shapes("countdown").TextFrame.TextRange = Format((Now() - time1), "hh:mm:ss") Loop End Sub
The text within the shape is the difference between the current time (which keeps increasing) and
time1 (constant: the time at which the code was run), and hence as the difference keeps widening, the count up occurs. The
loop continues until the current time becomes greater than