VBA Run Code non-stop

心已入冬 提交于 2019-12-25 16:39:09

问题


I am trying to write a program that runs indefinitely, till I stop it. For some reason it exited this while loop early (after 6 hours). A co-worker thinks it is because an error in the cpu, and I should use a Boolean flag instead of a string (less chance for the error). Is there any other suggestions?

If CommandButton1.Caption = "Start" Then
    CommandButton1.Caption = "Stop"
Else
    CommandButton1.Caption = "Start"
End If


While CommandButton1.Caption = "Stop"
    pause 1
    Range("C1").Value = Range("C1").Value + 1
    If Range("C1").Value = 300 Then
        Range("C1").Value = 0
        takeameasurement      ' This function did not cause the problem
    End If
Wend

(my pause function if you were wondering)

Function pause(time As Integer)
t = Timer + time      'wait for the table to settle before taking a measurement

Do While t > Timer
    DoEvents
Loop
End Function

回答1:


Timer returns the number of seconds since midnight, so you may run into a problem if you call your pause function just before Timer loops back to zero.

Imagine only 100 seconds in a day and you call pause when Timer = 99.5 - potentially you could end up with a value of t which is 100.5, in which case t > Timer will always be true and your Do loop never exits.

Function pause(time As Integer)
    t = Timer + time      

    Do While t > Timer
        DoEvents
    Loop
End Function


来源:https://stackoverflow.com/questions/31192195/vba-run-code-non-stop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!