打破一段时间...... Wend循环

Pri*_*kar 100 excel vba while-loop

我正在使用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
Run Code Online (Sandbox Code Playgroud)

我不想使用像`while count <= 10 ...这样的条件

Ale*_* K. 168

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

改为Do循环:

Do While True
    count = count + 1

    If count = 10 Then
        Exit Do
    End If
Loop
Run Code Online (Sandbox Code Playgroud)

或者循环设定次数:

for count = 1 to 10
   msgbox count
next
Run Code Online (Sandbox Code Playgroud)

(Exit For可以在上面用来提前退出)