[Excel VBA] Countdown timer in Excel

povictory

New Member
Joined
May 28, 2015
Messages
41
I'm trying to make a countdown timer in Excel similar to what is explained in this video:

https://www.youtube.com/watch?v=sbJeGG_Xv8M


I was able to re-create what is in the video, however I'm hoping someone here can help me with two modifications:



  1. With this code as it is, the clock stops if you do anything else in the Excel file while it's running. Anybody know a way around this so that the clock in the text box will still run while other cells are being manipulated? The author of the above video alludes to this problem in the comments and mentions a fix that he will follow up on, but hasn't updated it yet.
  2. Anybody know of a way to have this clock reset when a value is entered in multiple ranges? For example, in the following screenshot, I would want the clock to reset to a value (let's say 5 minutes) and begin counting down again as soon as any value is entered in any of the yellow cells (and reset over and over again as those cells become populated). Hope that makes sense.
Imgur

Any assistance on this would be greatly appreciated!! I've copied the code from the video below for reference:

Code:
Sub starttimer()
Application.OnTime Now + TimeValue("00:00:01"), "nexttick"
End Sub

Sub nexttick()
If Sheet1.Range("B1") = 0 Then Exit Sub
Sheet1.Range("B1").Value = Sheet1.Range("B1").Value - TimeValue("00:00:01")

If Sheet1.Range("B1").Value <= TimeValue("00:00:30") Then
    Sheet1.Shapes("TextBox 1").Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
    Sheet1.Shapes("TextBox 1").Fill.ForeColor.RGB = RGB(129, 229, 243)
End If

starttimer
End Sub

Sub stoptimer()
On Error Resume Next
Application.OnTime Now + TimeValue("00:00:01"), "nexttick", , False
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Just an update to this...

First of all just an FYI, I posted this question on another forum and I now understand that it is proper etiquette to link to the other post so here is that link:

Countdown timer in Excel that will run while spreadsheet is manipulated

Second, I asked Youtube user anameiwontforget about the fix he alluded to for part 1 of my question, and he graciously responded! So now part 1 has been addressed. I'm still looking for any suggestions on part 2. I've pasted the modified code below that allows the timer to keep running. Any ideas on how to have the clock reset after data is entered in a selected range?

Code:
Dim Starttime
Dim timerlength

Sub starttimer()
Starttime = Now()
timerlength = Sheet1.Range("b1")
Application.OnTime Now + TimeValue("00:00:01"), "nexttick"
End Sub

Sub looptimer()
Application.OnTime Now + TimeValue("00:00:01"), "nexttick"
End Sub

Sub nexttick()
If Sheet1.Range("b1") < TimeValue("00:00:01") Then
    Sheet1.Range("B1") = 0
    Exit Sub
End If
Sheet1.Range("b1").Value = timerlength - (Now() - Starttime)

If Sheet1.Range("B1").Value <= TimeValue("00:00:30") Then
    Sheet1.Shapes("TextBox 1").Fill.ForeColor.RGB = RGB(255, 0, 0)
Else
    Sheet1.Shapes("TextBox 1").Fill.ForeColor.RGB = RGB(129, 229, 243)
End If

looptimer
End Sub

Sub stoptimer()
On Error Resume Next
Application.OnTime Now + TimeValue("00:00:01"), "nexttick", , False
End Sub
 
Upvote 0

Forum statistics

Threads
1,207,189
Messages
6,076,991
Members
446,248
Latest member
tim_crouse

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top