web-dev-qa-db-fra.com

Sortez d'une boucle While ... Wend

J'utilise une boucle While ... Wend de VBA.

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

Je ne veux pas utiliser une condition comme `While count <= 10 ... Wend

98
Priyank Thakkar

Une boucle While/Wend ne peut être sortie prématurément qu'avec un GOTO ou en quittant un bloc externe (Exit sub/function ou une autre boucle existante)

Utilisez plutôt une boucle Do:

Do While True
    count = count + 1

    If count = 10 Then
        Exit Do
    End If
Loop

Ou pour boucler un nombre défini de fois:

for count = 1 to 10
   msgbox count
next

(Exit For peut être utilisé ci-dessus pour sortir prématurément)

166
Alex K.