Skip to content

Instantly share code, notes, and snippets.

@nmpowell
Created June 28, 2017 07:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmpowell/9e7a6e0a2d2c780da31a8be42772bc1e to your computer and use it in GitHub Desktop.
Save nmpowell/9e7a6e0a2d2c780da31a8be42772bc1e to your computer and use it in GitHub Desktop.
A countdown timer in VBA, for MS Excel. Copy-paste into a module in a .xlsm workbook.
Dim updatingTimeNow As Date
Private Sub Auto_Open()
' Run as the workbook opens
Call InitialiseTimer
End Sub
Private Sub SetUp()
' Run before anything else, independently
Cells(1, 1) = "Countdown timer"
Cells(2, 1) = "start time"
Cells(3, 1) = "active time-formatted cell"
Cells(4, 1) = "text conditional on timer"
Cells(2, 2) = "00:00:30"
End Sub
Sub InitialiseTimer()
Dim startTime As Range
Set startTime = Application.ActiveSheet.Range("B2")
Range("B3").Value = startTime
Call Timer
End Sub
Sub Timer()
updatingTimeNow = Now + TimeValue("00:00:01")
Application.OnTime updatingTimeNow, "ResetTime"
End Sub
Sub ResetTime()
Dim activeTime As Range
ThisWorkbook.Activate
Set activeTime = Application.ActiveSheet.Range("B3")
activeTime.Value = activeTime.Value - TimeSerial(0, 0, 1)
If activeTime.Value <= 0 Then
' MsgBox "Countdown complete."
Range("B4").Value = "Countdown complete."
Exit Sub
Else
Range("B4").Value = "Counting down ..."
End If
Call Timer
End Sub
@psvnmahesh
Copy link

Awesome, thank you.....:)

@ronan-deshays
Copy link

Hey ! Thank you for sharing your code ! It helped me a lot !

I ran into an issue when running your code as is on Excel 2019 : the countdown was slower than reality. But when I replaced activeTime.Value = activeTime.Value - TimeSerial(0, 0, 1) with activeTime.Value = activeTime.Value - TimeValue("00:00:01") this issue seems to be resolved.

Please let me know if you think, that it is a good solution :) !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment