打破While…Wend循环


107

我正在使用VBA的While ... Wend循环。

Dim count as Integer

While True
    count=count+1

    If count = 10 Then
        ''What should be the statement to break the While...Wend loop? 
        ''Break or Exit While not working
    EndIf
Wend

我不想使用类似`While count <= 10 ... Wend的条件

Answers:


176

While/ Wend循环只能与一个过早地退出GOTO或通过从外块(退出Exit sub/ function或另一exitable环)

更改为一个Do循环,而不是:

Do While True
    count = count + 1

    If count = 10 Then
        Exit Do
    End If
Loop

或循环设置次数:

for count = 1 to 10
   msgbox count
next

Exit For可在上方用于过早退出)


-1

另一个选择是将标志变量设置为a Boolean,然后根据您的条件更改该值。

Dim count as Integer 
Dim flag as Boolean

flag = True

While flag
    count = count + 1 

    If count = 10 Then
        'Set the flag to false         '
        flag = false
    End If 
Wend

-1

最好的方法是AndWhile语句中使用一个子句

Dim count as Integer
count =0
While True And count <= 10
    count=count+1
    Debug.Print(count)
Wend
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.