Exit Statement

Immediately moves control out of a procedure or loop.

Syntax

Exit { Do | For | Function | Method | Property | Sub | While }
or
Exit Program [ exitCode ]

Parts

Exit Do
Immediately stops the Do...Loop loop in which it appears. Execution continues with the statement following the Loop clause.
Exit For
Immediately stops the For or For Each loop in which it appears. Execution continues with the statement following the End For clause.
Exit Function
Immediately stops the Function procedure in which it appears. Execution continues with the statement that called the procedure, if the expression needs further evaluation; or the following statement otherwise.
Exit Method
Immediately stops the Method procedure in which it appears. Execution continues with the statement that called the procedure, if the expression needs further evaluation; or the following statement otherwise.
Exit Property
Immediately stops the Property procedure in which it appears.
Exit Sub
Immediately stops the Sub procedure in which it appears. Execution continues with the statement following the statement that called the procedure.
Exit While
Immediately stops the While loop in which it appears. Execution continues with the statement following the End While clause.
Exit Program
Immediately stops the Program module in which it appears. Execution returns to the process that started the program.
exitCode
Optional — an integer expression. If omitted, the default value is zero.

Details

When Exit is used in nested loops of the same type (e.g. two For loops), Exit exits the innermost loop and transfers control to the next higher level of nesting.

Examples

See also