我正在使用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
这样的条件答案 0 :(得分:164)
While
/ Wend
循环只能通过GOTO
过早退出,或退出外部区块Exit sub
/ function
或其他可退出的循环环)
改为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 :(得分:0)
另一种选择是将标志变量设置为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
答案 2 :(得分:-1)
最好的方法是在And
语句中使用While
子句
Dim count as Integer
count =0
While True And count <= 10
count=count+1
Debug.Print(count)
Wend