Dim alarm As Date
Dim start As Date
Dim sec As Integer
Dim min As Integer
Sub initialize()
' set default values
Range("B1:B2").MergeCells = True
Range("C1:C2").MergeCells = True
Range("B1:C2").Select
With Selection
'FormatConditions
.FormatConditions.Delete
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=$B$1+$C$1>0"
.FormatConditions(1).Interior.ColorIndex = 4
.FormatConditions.Add Type:=xlExpression, Formula1:="=$B$1+$C$1<=0"
.FormatConditions(2).Interior.ColorIndex = 3
' 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 = 24
End With
Rows("1:2").RowHeight = 20
'Add Start Button
ActiveSheet.Range("A1").Select
ActiveSheet.Shapes.AddShape(msoShapeRectangle, ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height).Select
With Selection
.ShapeRange.Fill.ForeColor.SchemeColor = 43
.Characters.Text = "Start"
.OnAction = "StartTimer"
.Font.Name = "Calibri"
.Font.Size = 14
.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
.ShapeRange.Fill.ForeColor.SchemeColor = 42
.Characters.Text = "Stop"
.OnAction = "StopTimer"
.Font.Name = "Calibri"
.Font.Size = 14
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
'
ActiveSheet.Range("B1").Value = 0
ActiveSheet.Range("C1").Value = 5
Call StartTimer
End Sub
Sub StartTimer()
'start timer, set values according to value on sheet
sec = ActiveSheet.Range("C1").Value
min = ActiveSheet.Range("B1").Value
ActiveSheet.Range("A2").Select
start = TimeValue(Now())
Call TrapTime
End Sub
Private Sub UpdateDisplay()
' updates display
ActiveSheet.Range("C1").Value = sec - Second(Now - start)
ActiveSheet.Range("B1").Value = min - Minute(Now - start)
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
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 modified Vog's code a bit to make a working stop watch. Open a NEW Excel workbook, open vb editor, add a module, paste in code. Run the initialize method and that is all. Sets up buttons for starting/stopping stopwatch. Enter the time you want to countdown in minutes/seconds.
Code:Dim alarm As Date Dim start As Date Dim sec As Integer Dim min As Integer Sub initialize() ' set default values Range("B1:B2").MergeCells = True Range("C1:C2").MergeCells = True Range("B1:C2").Select With Selection 'FormatConditions .FormatConditions.Delete .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=$B$1+$C$1>0" .FormatConditions(1).Interior.ColorIndex = 4 .FormatConditions.Add Type:=xlExpression, Formula1:="=$B$1+$C$1<=0" .FormatConditions(2).Interior.ColorIndex = 3 ' 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 = 24 End With Rows("1:2").RowHeight = 20 'Add Start Button ActiveSheet.Range("A1").Select ActiveSheet.Shapes.AddShape(msoShapeRectangle, ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height).Select With Selection .ShapeRange.Fill.ForeColor.SchemeColor = 43 .Characters.Text = "Start" .OnAction = "StartTimer" .Font.Name = "Calibri" .Font.Size = 14 .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 .ShapeRange.Fill.ForeColor.SchemeColor = 42 .Characters.Text = "Stop" .OnAction = "StopTimer" .Font.Name = "Calibri" .Font.Size = 14 .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter End With ' ActiveSheet.Range("B1").Value = 0 ActiveSheet.Range("C1").Value = 5 Call StartTimer End Sub Sub StartTimer() 'start timer, set values according to value on sheet sec = ActiveSheet.Range("C1").Value min = ActiveSheet.Range("B1").Value ActiveSheet.Range("A2").Select start = TimeValue(Now()) Call TrapTime End Sub Private Sub UpdateDisplay() ' updates display ActiveSheet.Range("C1").Value = sec - Second(Now - start) ActiveSheet.Range("B1").Value = min - Minute(Now - start) 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 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