GooberZoober
GooberZoober

Reputation: 13

Counter increment increases every time timer is restarted

I'm making a simple game in excel, and I have a count up timer, but every time I click to regenerate a new game, the increment the timer counts up by increases by one. For example, when it's opened, it goes up by one every second, but when I reset the game, it counts up by twos, and if I reset it again, it counts by threes.

Here is the code that generates a new game:

Dim Counting As Boolean
Sub New_Game()
'
' New_Game Macro
' Generates a new game
'
' Keyboard Shortcut: Ctrl+n
'
    Call StopTimer
    Range("C6:K14").Select
    Selection.ClearContents
    Range("AM1").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM2").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM3").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM4").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM5").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM6").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM7").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM8").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM9").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM10").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM11").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM12").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM13").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM14").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM15").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM16").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM17").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM18").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM19").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AM20").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN1").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN2").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN3").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN4").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN5").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN6").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN7").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN8").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN9").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN10").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN11").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN12").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN13").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN14").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN15").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN16").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN17").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN18").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN19").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AN20").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO1").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO2").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO3").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO4").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO5").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO6").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO7").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO8").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO9").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO10").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO11").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO12").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO13").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO14").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO15").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO16").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO17").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO18").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO19").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AO20").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP1").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP2").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP3").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP4").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP5").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP6").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP7").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP8").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP9").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP10").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP11").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP12").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP13").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP14").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP15").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP16").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP17").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP18").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP19").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP20").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("AP21").Select
    ActiveCell.FormulaR1C1 = "=staticRAND()"
    Range("A1").Select
    Call StartTimer
End Sub
Sub StartTimer()
    Counting = True
    Call Increment
End Sub
Sub StopTimer()
    Counting = False
    Range("AH2") = 0
End Sub
Sub Increment()
    If Counting Then
        Application.OnTime (Now + TimeValue("00:00:01")), "Increment"
        Range("AH2") = Range("AH2") + 1
    End If
End Sub

I can send the file if anyone needs it to understand what is happening.

Thanks

EDIT: Updated code:

Dim Counting As Boolean
Dim earliest As Variant
Sub New_Game()
'
' New_Game Macro
' Generates a new game
'
' Keyboard Shortcut: Ctrl+n
'
    Call StopTimer
    Range("C6:K14").Select
    Selection.ClearContents
    Range("AM1:AP20,AP21").Formula = "=staticRAND()"
    Range("C6").Select
    Call StartTimer
End Sub
Sub StartTimer()
    'Counting = True
    Call Increment
End Sub
Sub StopTimer()
    'Counting = False
    Range("AH2") = 0
    Application.OnTime earliest, "Increment", , False
End Sub
Sub Increment()
    'If Counting Then
        earliest = Now + TimeValue("00:00:01")
        Application.OnTime earliest, "Increment"
        Range("AH2") = Range("AH2") + 1
    'End If
End Sub

Now I'm getting a run-time error: Method 'OnTime' of object'_Application' failed

EDIT:

Opened it up this morning and it works perfectly.

Upvotes: 0

Views: 291

Answers (1)

Andy G
Andy G

Reputation: 19367

Your NewGame() procedure calls StopTimer and later calls StartTimer. This will all happen much faster than 1 second, so the timer is never stopped - so you have two timers running, then, three, which is why the number increases.

I believe you need to explicitly cancel the previous timer using the Schedule argument of the OnTime method.

expression.OnTime(EarliestTime, Procedure, LatestTime, Schedule)

You need a global variable to store the EarliestTime. This is necessary to cancel the timer based on both the procedure's name and its earliest time:

Dim earliest As Variant

In your Increment procedure you set this variable:

earliest = Now + TimeValue("00:00:01")
Application.OnTime earliest, "Increment"

Then, in your StopTimer procedure, you can cancel the previous timer:

Application.OnTime earliest, "Increment", , False

There is probably no need for your Counting variable now.

Upvotes: 2

Related Questions