循环播放OnTime事件会导致Excel崩溃

时间:2018-04-06 03:49:50

标签: excel vba excel-vba

以下是我在模块中添加的内容:

Public Times As Boolean
Sub start()

Times = True

Track:
Application.OnTime Now() + TimeValue("00:00:01"), "Run"
If Times = True Then GoTo Track

End Sub
Sub run()

Range("E3").Value = Range("E3").Value + TimeValue("00:00:01")

End Sub
Sub Tend()

Times = False

End Sub

现在,当我运行Start()时,我的Excel崩溃了。 请咨询

1 个答案:

答案 0 :(得分:3)

你在Start中的代码是一个紧凑的循环,这不是我想要做的。

Public Times As Boolean

Sub start()
    Times = True
    Run    
End Sub

Sub run()
    If Not Times Then Exit Sub  
    Application.OnTime Now() + TimeValue("00:00:01"), "Run"
    Range("E3").Value = Range("E3").Value + TimeValue("00:00:01")
End Sub

Sub Tend() 
    Times = False
End Sub