I have a request to program one cell of a spreadsheet that
the user can type a minute value (i.e. 00:10:00) and when
they press the enter key (with focus on this cell) a timer
starts and the display changes to count down to 00:00:00.
The value will be entered in minutes, so if there is any
way that the user just has to enter "10" instead of the
entire 8 character time format, that would be ideal.
Another way. The following code goes into the code module for the worksheet
whose A1 cell is the countdown timer. Both the Change and the CountDown
procedure go into the same module. If the user wants to abort the countdown, all
s/he has to do is clear the A1/countdown timer cell's contents or enter anything
other than a positive number.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim t As Double
If Target.Address(0, 0) <> "A1" Then Exit Sub
On Error Resume Next
t = Target.Value2
On Error GoTo 0
If t > 0 Then Me.CountDown
End Sub
Sub CountDown()
Static repcall As Boolean, nf As String, t As Date
Dim mer As Range, merv As Double
Set mer = Me.Range("A1")
On Error Resume Next
merv = mer.Value2
On Error GoTo CleanUp
Application.EnableEvents = False
If Not repcall Then
'Debug.Print "begun at", Now
repcall = True
nf = mer.NumberFormat
t = Now + TimeSerial(0, merv, 0)
mer.NumberFormat = "mm:ss"
mer.Value = TimeSerial(0, merv, 0)
ElseIf t > Now And merv > 0 Then
mer.Value = t - Now
Else
repcall = False
If merv > 0 Then mer.Value = "Time's up!"
mer.NumberFormat = nf
'Debug.Print "done at", Now
End If
If repcall Then Application.OnTime _
EarliestTime:=Now + TimeSerial(0, 0, 1), _
Procedure:=Me.Name & ".CountDown"
CleanUp:
Application.EnableEvents = True
End Sub