what exactly does Exit sub do?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have:

Sub A

Call Sub B

End Sub A

Sub B

On Error Goto ErrH
exit sub

ErrH:
.....

end Sub B

The exit sub seems to exit out of Sub A too. Why is that?
 
The reason it exits sub A is that there is no following code in A :
The exit sub in Sub B returns control to A.
The next line of code in A is End Sub, therefore all processing stops.
The exit sub only ends that sub routine, but if there is no code in the sub
above, it
"flows" to the end of the routine.
Try modifying the sub a by adding a line like debug.print "Now in Sub A"
after the call sub B. You should see the output in the immediate window of
your VB environment, or at least that line should be highlighted in your
debug routine if you are using single steps (F8) to process the routine.
Hope that helps
MjM
 
Note the two statements I added to your example that are encased in triple
asterisks...
Sub B

On Error Goto ErrH
***Executing Code***
exit sub

ErrH:
***Line 1 Of The Error Handler Code***
....

end Sub B

Just so you understand why the Exit Sub statement is in this piece of
code... what it does is prevent the main body's code from entering the error
handler code below it. If you did not have the Exit Sub statement, then the
Executing Code, when finished, would continue with Line 1 Of The Error
Handler Code... the Exit Sub stops the Executing Code from continuing on
into the code marked by the label ErrH: and simply ends the execution of the
subroutine. That way, the error handler code will only be executed when an
error occurs.

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top