我在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
現在我得到一個運行時錯誤:object'_Application的方法 '準時'」失敗
編輯:
今天早上打開它,它完美的作品。
我還沒有看過你的問題,但整個大段的代碼可以只是'範圍(「AM1:AP20,AP21」)。公式=「= staticRAND()」' –
好的謝謝。這是有道理的,並使其更好。而且運行速度更快。 – GooberZoober