Exit a while loop in VBS/VBA

前端 未结 6 1776
天涯浪人
天涯浪人 2020-12-11 14:25

Is there a method of exiting/breaking a while in VBS/VBA?

Following code won\'t work as intended:

num = 0
while (num < 10)

    if (s         


        
6条回答
  •  有刺的猬
    2020-12-11 15:10

    Incredibly old question, but bearing in mind that the OP said he does not want to use Do While and that none of the other solutions really work... Here's something that does exactly the same as a Exit Loop:

    This never runs anything if the status is already at "Fail"...

    While (i < 20 And Not bShouldStop)
        If (Status = "Fail") Then
            bShouldStop = True
        Else
            i = i + 1
            '
            ' Do Something
            '
        End If  
    Wend
    

    Whereas this one always processes something first (and increment the loop variable) before deciding whether it should loop once more or not.

    While (i < 20 And Not bShouldStop)
        i = i + 1
        '
        ' Do Something
        '
    
        If (Status = "Fail") Then
            bShouldStop = True
        End If  
    Wend
    

    Ultimately, if the variable Status is being modified inside the While (and assuming you don't need i outside the while, it makes no difference really, but just wanted to present multiple options...

提交回复
热议问题