Exit Statement
The Exit statement is a control structure that stops statements that reside in a loop or transfers control to a calling procedure. It does not return a value.
You can include an Exit Do statement in a Do Loop statement. You can use an Exit For statement in a For Next statement. When the Exit statement runs, control transfers to the statement that occurs after the Loop statement or the Next statement. When used in a nested loop, an Exit statement moves control out of the immediately enclosing loop.
Note the following:
You can use the Exit statement in the Create Function method. For more information, see Create Function Method.
You can use the Exit Sub statement in the Create Subroutine method. For more information, see Create Subroutine Method.
Format
Exit {Do | For | Function | Sub}
The following example uses the On Error statement to handle run-time errors. If an error exists, then the code continues at the Debugger label. This example uses the Exit statement to skip the debugging code when no error exists:
Sub Button_Click
Dim msgtext, userfile
On Error GoTo Debugger
msgtext = "Enter the filename to use:"
userfile = "c:\temp\trace.txt"
Open userfile For Input As #1
' ....etc....
Close #1
done:
Exit Sub
Debugger:
msgtext = "Error " & Err & ": " & Error$
Resume done
End Sub