我正在用excel制作一个简单的游戏,并且我有一个计时计时器,但每次我点击重新生成一个新游戏时,计时器的增量会增加一个。例如,当它打开时,它每秒上升一次,但是当我重置游戏时,它会增加两倍,如果我再次重置它,那就算三分。
以下是生成新游戏的代码:
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
如果有人需要它,我可以发送文件来了解发生了什么。
谢谢
编辑: 更新的代码:
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
现在我遇到了运行时错误:对象'_Application'的方法'OnTime'失败
编辑:
今天早上打开它,效果很好。
答案 0 :(得分:2)
您的NewGame()过程调用StopTimer,稍后调用StartTimer。这一切都会比1秒快得多,所以计时器永远不会停止 - 所以你有两个计时器运行,然后是三个,这就是数字增加的原因。
我认为您需要使用Schedule
方法的OnTime
参数明确取消之前的计时器。
expression.OnTime(EarliestTime,Procedure,LatestTime,Schedule)
您需要一个全局变量来存储EarliestTime
。这是根据程序名称及其最早时间取消计时器所必需的:
Dim earliest As Variant
在增量程序中设置此变量:
earliest = Now + TimeValue("00:00:01")
Application.OnTime earliest, "Increment"
然后,在StopTimer过程中,您可以取消之前的计时器:
Application.OnTime earliest, "Increment", , False
现在可能不需要Counting
变量。