SoldierofJustice
New Member
- Joined
- Dec 1, 2012
- Messages
- 2
Hello all,
I needed to create a stopwatch in Excel. I found one on these forums located here: http://www.mrexcel.com/forum/excel-questions/534628-create-stopwatch-excel-2.html
It did almost everything I needed, however I wanted the cells to change color to yellow when the timer hit 30 seconds and red when it hit zero. I tried playing around with the code, but now the cells change color every time C1 hits 30 seconds (i.e. 2:30, 1:30.) The code is as follows:
I placed the color changing code in UpdateDisplay.
It has been over a decade since I did any programming, so I am really fumbling through this. Any help you all can give would be greatly appreciated.
Thank you.
I needed to create a stopwatch in Excel. I found one on these forums located here: http://www.mrexcel.com/forum/excel-questions/534628-create-stopwatch-excel-2.html
It did almost everything I needed, however I wanted the cells to change color to yellow when the timer hit 30 seconds and red when it hit zero. I tried playing around with the code, but now the cells change color every time C1 hits 30 seconds (i.e. 2:30, 1:30.) The code is as follows:
Code:
Dim alarm As Date
Sub initialize()
' set default values
Range("B1:B2").MergeCells = True
Range("C1:C2").MergeCells = True
Range("B1:C2").Select
With Selection
' Borders
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlInsideVertical).LineStyle = xlContinuous
' Text Alignment
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
' Font
.Font.Name = "Calibri"
.Font.Size = 28
End With
Rows("1:2").RowHeight = 40
Rows("1,3").ColumnWidth = 20
'Add Start Button
ActiveSheet.Range("A1").Select
ActiveSheet.Shapes.AddShape(msoShapeRectangle, ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height).Select
With Selection
.Characters.Text = "Start"
.OnAction = "StartTimer"
.Font.Name = "Calibri"
.Font.Size = 22
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
'Add Stop Button
ActiveSheet.Range("A2").Select
ActiveSheet.Shapes.AddShape(msoShapeRectangle, ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height).Select
With Selection
.Characters.Text = "Stop"
.OnAction = "StopTimer"
.Font.Name = "Calibri"
.Font.Size = 22
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
ActiveSheet.Range("B1").Value = 0
ActiveSheet.Range("C1").Value = 0
Call StartTimer
End Sub
Sub StartTimer()
'start timer, set values according to value on sheet
Call TrapTime
End Sub
Private Sub UpdateDisplay()
' updates display
ActiveSheet.Range("C1").Value = ActiveSheet.Range("C1").Value - 1
If ActiveSheet.Range("C1").Value < 0 Then
ActiveSheet.Range("B1").Value = ActiveSheet.Range("B1").Value - 1
ActiveSheet.Range("C1").Value = ActiveSheet.Range("C1").Value + 60
If ActiveSheet.Range("B1").Value > 0 Then
Range("B1:C2").Interior.Color = 4
ElseIf ActiveSheet.Range("B1").Value = 0 And ActiveSheet.Range("C1") <= 30 Then
Range("B1:C2").Interior.Color = 6
ElseIf ActiveSheet.Range("B1").Value = 0 And ActiveSheet.Range("C1") = 0 Then
Range("B1:C2").Interoir.Color = 3
End If
End If
Call TrapTime
End Sub
Private Sub TrapTime()
If ActiveSheet.Range("B1").Value = 0 And ActiveSheet.Range("C1").Value = 0 Then
StopTimer
Exit Sub
End If
alarm = CDate(Date) + TimeValue(Now()) + TimeValue("00:00:01")
Application.OnTime earliesttime:=alarm, procedure:="UpdateDisplay"
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliesttime:=alarm, procedure:="UpdateDisplay", schedule:=False
End Sub
I placed the color changing code in UpdateDisplay.
It has been over a decade since I did any programming, so I am really fumbling through this. Any help you all can give would be greatly appreciated.
Thank you.