I hope this does what I wrote it to do. First, make sure you have a
Settings Sheet where the combat time in minutes is set (e.g., cell A1 contains the initial countdown time). You will also need a
checkbox for Golden Score mode.
VBA Code:
Dim CountdownTime As Integer
Dim TimerRunning As Boolean
Dim GoldenScoreMode As Boolean
Dim CurrentTime As Integer
Dim StartTime As Integer
Dim CountDownTimer As Double
Sub StartTimer()
Dim combatTime As Integer
combatTime = ThisWorkbook.Sheets("Settings").Range("A1").Value * 60
If GoldenScoreMode Then
CurrentTime = 0
CountdownTime = 999999
Else
CurrentTime = combatTime
CountdownTime = combatTime
End If
TimerRunning = True
Call UpdateTimerDisplay
CountDownTimer = Application.OnTime(Now + TimeValue("00:00:01"), "TimerTick")
End Sub
Sub TimerTick()
If Not TimerRunning Then Exit Sub
Select Case GoldenScoreMode
Case False
If CurrentTime > 0 Then
CurrentTime = CurrentTime - 1
Else
Call TimerEnded
End If
Case True
CurrentTime = CurrentTime + 1
Case Else
MsgBox "Unexpected mode"
End Select
Call UpdateTimerDisplay
CountDownTimer = Application.OnTime(Now + TimeValue("00:00:01"), "TimerTick")
End Sub
Sub StopTimer()
TimerRunning = False
On Error Resume Next
Application.OnTime CountDownTimer, "TimerTick", , False
On Error GoTo 0
Call TimerEnded
End Sub
Sub PauseTimer()
TimerRunning = False
End Sub
Sub ResetTimer()
If Not GoldenScoreMode Then
CurrentTime = CountdownTime
Else
CurrentTime = 0
End If
Call UpdateTimerDisplay
End Sub
Sub TimerEnded()
Dim soundPath As String
soundPath = ThisWorkbook.Path & "\alarm.wav"
Call PlaySound(soundPath)
Call DetermineWinner
End Sub
Sub PlaySound(soundPath As String)
Dim objShell As Object
Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /c start """" """ & soundPath & """", 0, False
Set objShell = Nothing
End Sub
Sub UpdateTimerDisplay()
Dim Minutes As Long, Seconds As Long, DisplayTime As String
Minutes = Int(CurrentTime / 60)
Seconds = CurrentTime Mod 60
DisplayTime = Format(Minutes, "00") & ":" & Format(Seconds, "00")
ThisWorkbook.Sheets("Scoreboard").Range("B1").Value = DisplayTime
End Sub
Sub DetermineWinner()
Dim WhiteScore As Long, BlueScore As Long
With ThisWorkbook.Sheets("Scoreboard")
WhiteScore = .Range("B2").Value
BlueScore = .Range("B3").Value
Select Case WhiteScore
Case Is > BlueScore
.Range("B4").Value = "White wins"
Case Is < BlueScore
.Range("B4").Value = "Blue wins"
Case Else
.Range("B4").Value = "Draw"
End Select
End With
End Sub
Sub ToggleGoldenScoreMode()
With ThisWorkbook.Sheets("Scoreboard").Range("A5")
GoldenScoreMode = Not GoldenScoreMode
If GoldenScoreMode Then
.Value = "Golden Score"
Else
.Value = "Normal"
End If
End With
End Sub
If you are really smart, you can create a userform that acts like a standalone application instead of using spreadsheets for dynamic updates. That's a little more complex but so much more user-friendly than a spreadsheet and you'll get some golden looks for your efforts.
To integrate this timer functionality into a UserForm in Excel, you'll need to take several steps. The following process shows how you can use the code inside a UserForm to control the timer, pause, stop, reset, and toggle between normal and Golden Score modes.
Steps:
Create a UserForm with necessary controls:
Add labels, text boxes, buttons, and checkboxes to the form:
Timer Display: A label to show the countdown (minutes and seconds).
Start Button: To start the timer.
Pause Button: To pause the timer.
Stop Button: To stop the timer and play the sound.
Reset Button: To reset the timer.
Golden Score Toggle: A checkbox to toggle Golden Score mode.
Link VBA code to the UserForm: We'll modify the provided VBA code so that it operates within the UserForm environment. Here's how you can integrate your timer logic into the UserForm.
Code to put in UserForm1:
VBA Code:
Dim CountdownTime As Integer
Dim TimerRunning As Boolean
Dim GoldenScoreMode As Boolean
Dim CurrentTime As Integer
Dim StartTime As Integer
Dim CountDownTimer As Double
Private Sub UserForm_Initialize()
Dim combatTime As Integer
combatTime = ThisWorkbook.Sheets("Settings").Range("A1").Value * 60
CountdownTime = combatTime
CurrentTime = CountdownTime
GoldenScoreMode = False
TimerRunning = False
UpdateTimerDisplay
End Sub
Private Sub StartButton_Click()
If TimerRunning Then Exit Sub
TimerRunning = True
Call UpdateTimerDisplay
CountDownTimer = Application.OnTime(Now + TimeValue("00:00:01"), "TimerTick")
End Sub
Sub TimerTick()
If Not TimerRunning Then Exit Sub
Select Case GoldenScoreMode
Case False
If CurrentTime > 0 Then
CurrentTime = CurrentTime - 1
Else
Call TimerEnded
End If
Case True
CurrentTime = CurrentTime + 1
Case Else
MsgBox "Unexpected mode"
End Select
Call UpdateTimerDisplay
CountDownTimer = Application.OnTime(Now + TimeValue("00:00:01"), "TimerTick")
End Sub
Private Sub StopButton_Click()
TimerRunning = False
On Error Resume Next
Application.OnTime CountDownTimer, "TimerTick", , False
On Error GoTo 0
Call TimerEnded
End Sub
Private Sub PauseButton_Click()
TimerRunning = False
End Sub
Private Sub ResetButton_Click()
If Not GoldenScoreMode Then
CurrentTime = CountdownTime
Else
CurrentTime = 0
End If
Call UpdateTimerDisplay
End Sub
Sub TimerEnded()
Dim soundPath As String
soundPath = ThisWorkbook.Path & "\alarm.wav"
Call PlaySound(soundPath)
Call DetermineWinner
End Sub
Sub PlaySound(soundPath As String)
Dim objShell As Object
Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /c start """" """ & soundPath & """", 0, False
Set objShell = Nothing
End Sub
Sub UpdateTimerDisplay()
Dim Minutes As Long, Seconds As Long, DisplayTime As String
Minutes = Int(CurrentTime / 60)
Seconds = CurrentTime Mod 60
DisplayTime = Format(Minutes, "00") & ":" & Format(Seconds, "00")
TimerDisplayLabel.Caption = DisplayTime
End Sub
Sub DetermineWinner()
Dim WhiteScore As Long, BlueScore As Long
With ThisWorkbook.Sheets("Scoreboard")
WhiteScore = .Range("B2").Value
BlueScore = .Range("B3").Value
Select Case WhiteScore
Case Is > BlueScore
.Range("B4").Value = "White wins"
Case Is < BlueScore
.Range("B4").Value = "Blue wins"
Case Else
.Range("B4").Value = "Draw"
End Select
End With
End Sub
Private Sub GoldenScoreCheckBox_Click()
GoldenScoreMode = Not GoldenScoreMode
If GoldenScoreMode Then
GoldenScoreLabel.Caption = "Golden Score Mode"
Else
GoldenScoreLabel.Caption = "Normal Mode"
End If
End Sub
And then run your userform. You can make the form do all sorts of intersting things like change colour to gold if you want!
You can add images as backgrounds and you can really make it look fantastic just to blow the minds of people who know you and Excel programming. Userforms are not used often enough in VBA.
Now go play nicely!