Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Countdown timer in Excel

Posted on 2007-03-19
7
1,437 Views
Last Modified: 2008-02-01
Not sure if this sent properly last time,so forgive me if it gets posted twice.

I want to display a countdown timer in a cell on an excel worksheet (or it could be in a box/form) that counts down in hours, minutes and seconds. Basically, the user enters the time allowed in one cell (say 3 hours in the format 03:00:00), presses a command button, and the timer counts down in another cell. It should also display the elapsed time (in seconds) in a third cell.

So, for example, you type 03:00:00 in A1, the countdown timer displays 03:00:00, 02:59:59, 02::58:58, etc. in A2 and 01, 02, 03, etc. in A3.

It is for a game and the user must always be able to see the timer, even if they are doing something else on the sheet. Also, the third cell must be in seconds because it will be used later to deduct credits for every second they use up.

Thanks

Terry
0
Comment
Question by:Terrygordon
  • 3
  • 2
  • 2
7 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 250 total points
ID: 18749962
Terry,
Here is some code for a countdown timer. You start the timer by running sub StartTimer. You stop it by running sub StopTimer. The starting value for the timer goes in cell A1 and the timer itself goes in cell B1. You'll need to format these cells as desired. The elapsed time goes in C1.

Public booOnTime As Boolean       'Must go before any subs or functions in module sheet
Public RunWhen As Double
Public celDuration As Range, celTimer As Range, celElapsed As Range
Public TimerInterval As Double

Sub CountdownTimer()          'Goes in regular module sheet
celTimer = celTimer - TimerInterval
celElapsed = Round(86400 * (celDuration - celTimer), 0)
' Reset The Timer for next Time Check '
If booOnTime Then
      RunWhen = Now + TimeSerial(0, 0, 1)
      Application.OnTime RunWhen, "CountdownTimer"
End If
End Sub

Sub StartTimer()      'Goes in code pane for your worksheet
Set celDuration = [A1]  'Starting value of timer goes here
Set celTimer = [B1]     'Value of timer goes here
Set celElapsed = [C1]   'Elapsed time goes here

celTimer = celDuration  'Initial value of timer goes here
celElapsed = Round(86400 * (celDuration - celTimer), 0)
TimerInterval = 1 / 86400   '1 second interval

booOnTime = True
RunWhen = Now + TimerInterval
Application.OnTime RunWhen, "CountdownTimer"
End Sub

Sub StopTimer()         'Goes in regular module sheet
booOnTime = False
Application.OnTime earliesttime:=RunWhen, _
     procedure:="CountdownTimer", schedule:=False
End Sub


You will probably also want to call the StopTimer sub when the workbook is closed.
Sub Workbook_BeforeClose()       'Goes in ThisWorkbook code pane
  StopTimer
End Sub


Brad
0
 
LVL 13

Assisted Solution

by:WJReid
WJReid earned 100 total points
ID: 18750006
0
 

Author Comment

by:Terrygordon
ID: 18755302
Thanks Brad - Brilliant as usual, and I particularly liked the fact that you explained which sheets to put the code in and the inclusion of the 'stop on workbook close'. Incidentally, I have used the trick/advice you gave me about 2 years ago and put the timer and elapsed seconds into autoshapes, by linking them to the cells. So basically I end up with displays that actually look like digital clocks.

Bill - Your solution was also good, and did have the actual spreadsheet, but I'm afraid it was just a little later than Brads and it didn't have the second counter (i.e. your format was 00:02:10, as opposed to 130 seconds).

However, for the sake of fairness (what's 5 minutes between friends) I am increasing the points to 350 with 250 to Brad and 100 to Bill. I am sure that other users will benefit from both solutions.

Regards

Terry
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 

Author Comment

by:Terrygordon
ID: 18755332
Sorry. Forgot to increase the points.
0
 
LVL 13

Expert Comment

by:WJReid
ID: 18755368
Hi Terry,

Thank you very much for increasing the points and giving some to me, very kind of you since Brad was in first.

Bill
0
 

Author Comment

by:Terrygordon
ID: 18756069
Brad

When I go to close the workbook I am getting the following error message:

"Procedure declaration does not match description of event or procedure having the same name"

The compiler points to the workbook sub:

Sub Workbook_BeforeClose()       'Goes in ThisWorkbook code pane
  StopTimer
End Sub

which is obviously in the This Workbook code pane.

Any thoughts?

Regards

Terry
0
 
LVL 81

Expert Comment

by:byundt
ID: 18756410
Terry,
I forgot the Cancel parameter:

Private Sub Workbook_BeforeClose(Cancel As Boolean)       'Goes in ThisWorkbook code pane
  StopTimer
End Sub



You may also get an error if the timer has already been stopped. The fix for this is:

Sub StopTimer()         'Goes in regular module sheet
On Error Resume Next
booOnTime = False
Application.OnTime earliesttime:=RunWhen, _
     procedure:="CountdownTimer", schedule:=False
On Error GoTo 0
End Sub



Brad
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question