Created
June 28, 2017 07:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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 :) !
I was having trouble getting it to work the way I wanted even though I made it myself, so I referred to this countdown as a template and it worked easily and saved me a lot of trouble. Thank you very much!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, thank you.....:)