Name
Exit Statement
Syntax
Exit Do Exit For Exit Function Exit Property Exit Sub
Description
Prematurely exits a block of code.
Rules at a Glance
-
ExitDo Exits a
Do...Loopstatement. If the currentDo...Loopis within a nestedDo ...Loop, execution continues with the nextLoopstatement wrapped around the current one. If, however, theDo...Loopis standalone, program execution continues with the first line of code after theLoopstatement.-
ExitFor Exits a
For...Nextloop. If the currentFor...Nextis within a nestedFor ...Nextloop, execution continues with the nextNextstatement wrapped around the current one. If, however, theFor...Nextloop is standalone, program execution continues with the first line of code after theNextstatement.-
ExitFunction Exits the current function. Program execution is passed to the line following the call to the function.
-
ExitProperty Exits the current property procedure. Program execution is passed to the line following the call to the property.
-
ExitSub Exits the current sub procedure. Program execution is passed to the line following the call to the procedure.
Programming Tips & Gotchas
Traditional programming theory recommends one entry point and one
exit point for each procedure. However, you can improve the
readability of long routines by using the Exit
statement, as shown next. Using Exit
Sub can save having to wrap almost an entire
subroutine (which could be tens of lines long) within an
If...Then statement.
With Exit Sub
Sub MyTestSub(iNumber ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access