Rompi un po '... Wend loop


107

Sto usando un ciclo While ... Wend di 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

Non voglio usare condizioni come `While count <= 10 ... Wend

Risposte:


176

A While/ Wendciclo può essere abbandonato solo prematuramente con una GOTOo uscendo da un blocco esterno ( Exit sub/ functiono un altro ciclo exitable)

DoPassa invece a un ciclo:

Do While True
    count = count + 1

    If count = 10 Then
        Exit Do
    End If
Loop

O per ripetere un certo numero di volte:

for count = 1 to 10
   msgbox count
next

( Exit Forpuò essere usato sopra per uscire prematuramente)


-1

Un'altra opzione sarebbe quella di impostare una variabile flag come a Booleane quindi modificare quel valore in base ai criteri.

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

Il modo migliore è usare una Andclausola nella tua Whiledichiarazione

Dim count as Integer
count =0
While True And count <= 10
    count=count+1
    Debug.Print(count)
Wend
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.