by Dinesh Thakur Category: Data Type

The Exit statement allows you to exit prematurely from a block of statements in a control structure from a loop, or even from a procedure. Suppose you have a For…….Next loop that calculates the square root of negative numbers can't be calculated ( the Sqr() function generates a runtime error,)


you might want to halt the operation if the array contains a invalid value. To exit the loop prematurely, use the Exit For statement as follows :


For i=0 to UBound(nArray())

If nArray(i)<0 then Exit For




If a negative element is found in this loop, the program exits the loop and continues with the statement following the Next statement.


There are similar Exit statements for the Do loop (Exit Do), as well as for functions and subroutines (Exit Function and Exit Subroutine).