循环停止条件

时间:2016-02-24 07:08:01

标签: excel-vba loops vba excel

我有一个带开始和停止按钮的倒计时宏。我想修复停止时间,所以当计数器为零时,它不应该再次启动,因此应该替换Loop Until StopTimer。我尝试EndTime - Now = 0作为条件,但它不起作用。

Sub Countdown()
  Const Seconds = 10
  Dim EndTime As Double
  StopTimer = False
  Do
    If EndTime - Now < 0 Then
      EndTime = Now + TimeSerial(0, 0, Seconds)
    End If
    Range("A1") = EndTime - Now
    DoEvents
  Loop Until StopTimer
End Sub

1 个答案:

答案 0 :(得分:1)

Sub Countdown()
  Const Seconds = 10
  Dim EndTime As Double
  EndTime = Now + TimeSerial(0, 0, Seconds)
  StopTimer = False
  Do
    If EndTime - Now <= 0 Then
      StopTimer = True
    End If
    Range("A1") = EndTime - Now
    DoEvents
  Loop Until StopTimer
End Sub