Saltar al contenido

vba break while ejemplo de código de bucle

Ejemplo 1: salida de Excel vba mientras se realiza el bucle

'VBA does NOT have an Exit statement for While Wend loops. While Wend
'loops must run through completion:
While i < 1000
    c = c + 1
Wend

'...or be interrupted by a GoTo statement:  
While i < 1000
    c = c + 1
    If c = 750 Then GoTo MyExit
Wend

MyExit:
'But using a GoTo statement is usually bad coding practice.

Ejemplo 2: bucle while vba

i = 0
While i < 100
    'Code here is executed 100 times.
    i = i + 1 
Wend

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Notes: The While loop is terminated with the 'Wend' keyword.
'
'       The While loop must include a Boolean expression directly after
'       the 'While' keyword and the loop will execute for as long as the 
'       Boolean expression evaluates to True. This expression should be
'       based on something that has the POTENTIAL TO CHANGE on each cycle
'       through the loop. For example, 'i' in the above code.
'  
'	    The While expression is evaluated at the beginning EACH cycle through 
'       the loop. The expression DOES NOT NEED to be NUMERIC; but it must 
'       evaluate to True or False. The While loop will be skipped entirely if 
'       the expression evaluates to False before the loop begins.
  
'       The following code demonstrates how the above While loop
'       functions (without actually using the 'While' and 'When' keywords):

i = 0
LoopStart:
    If (i < 100) = False Then GoTo LoopEnd
        'Code here is executed 100 times.
        i = i + 1
    GoTo LoopStart
LoopEnd:  

'      This is for demonstration purposes only. Although these two loops are
'      equivalent, using the 'GoTo' statement should be avoided. The While loop
'      accomplishes the exact same task, but cleanly, without the 
'      line labels and without the 'GoTo' statements.
'
'Note: VBA does not include an 'Exit While' statement to terminate the loop
'      early. VBA does include the 'Exit Do' statement for the Do loop and 
'      also the 'Exit For' statement for the For loop. But it is possible
'      to exit the While loop early with a 'GoTo' statement, but again, the 
'      'GoTo' statement should be avoided. It's also possible to exit
'      the While loop with the 'Exit Sub' and 'Exit Function' statements.
'      But of course, these last two statements will not only exit the
'      While loop, they will also immediately exit the current procedure.
'    
'Note: From an efficiency standpoint, the For loop may be a better choice
'      than the While loop because the For loop's range is calculated only
'      once, before the For loop starts. By contrast, the While loop's
'      expression is evaluated before each and every cycle of the loop.
'
'
'

Ejemplo 3: vba para descanso

Dim i As Integer
For i = 0 To 10
    If i > 5 Then
        Debug.Print "Exit."
        Exit For
    End If
Next i
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)


Tags : /

Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *