Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
J@Y J@Y is offline
external usenet poster
 
Posts: 127
Default what exactly does Exit sub do?

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default what exactly does Exit sub do?

It exits the sub or macro - stops the macro from running

"J@Y" wrote:

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?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default what exactly does Exit sub do?

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

"J@Y" wrote:

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?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default what exactly does Exit sub do?

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exit Sub Jeff Excel Discussion (Misc queries) 2 March 1st 08 06:21 PM
End and Exit Sub clara Excel Programming 3 May 17th 07 12:16 AM
Run when exit PH NEWS Excel Worksheet Functions 1 July 18th 06 03:53 PM
how to exit sub xiang Excel Programming 3 December 9th 05 05:46 AM
If a called sub exit, how to the caller exit right away? luvgreen[_4_] Excel Programming 4 February 24th 04 05:06 PM


All times are GMT +1. The time now is 07:29 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"