Exit For (statement)

Syntax

Exit For

Description

Causes execution to exit the innermost For loop, continuing execution on the line following the Next statement.

Comments

This statement can only appear within a For...Next block.

Example

This example enters a large user-defined cycle, performs a calculation and exits the For...Next loop when the result exceeds a certain value.

Const critical_level = 500

Sub Main()
  num = InputBox("Please enter the number of cycles","Cycles")
  For i = 1 To Val(num)
    newpressure = i * 2
    If newpressure >= critical_level Then Exit For
  Next i

  MsgBox "The valve pressure is: " & newpressure
End Sub

See Also

Stop (statement); Exit Do (statement); Exit Function (statement); Exit Sub (statement); End (statement); Do...Loop (statement).

More information

E