Sub DoSomething()
GoSub DoThis
DoThis:
Debug.Print "Hi!"
Return
End Sub
Execution enters the DoSomething
procedure, jumps to the DoThis
label, prints "Hi!" to the debug output, returns to the instruction immediately after the GoSub
call, prints "Hi!" again, and then encounters a Return
statement, but there's nowhere to return to now, because we didn't get here with a GoSub
statement.
Sub DoSomething()
GoSub DoThis
Exit Sub
DoThis:
Debug.Print "Hi!"
Return
End Sub
By introducing an Exit Sub
instruction before the DoThis
line label, we have segregated the DoThis
subroutine from the rest of the procedure body - the only way to execute the DoThis
subroutine is via the GoSub
jump.
GoSub
/Return
is deprecated, and should be avoided in favor of actual procedure calls. A procedure should not contain subroutines, other than error handlers.
This is very similar to Run-time error '20': Resume without error; in both situations, the solution is to ensure that the normal execution path cannot enter a sub-routine (identified by a line label) without an explicit jump (assuming On Error GoTo
is considered an explicit jump).