Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 252
Default End and Exit Sub

Hi all,

what is the difference between End and Exit Sub if they are used in a Sub?
What state will be in after using them each?

Clara


--
thank you so much for your help
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default End and Exit Sub

Exit sub does exactly what it says... it exits the current sub. If the sub
was called by another sub then that sub will continue on as normal. Exit sub
does NOT halt all execution. It just exit the current sub.

End halts ALL exectution. While this sounds tempting to do it also clears
all global and static variables. It is the equivalent of stopping your car by
steering it into a tree. Your car will stop but the consequences are
significant. VERY RARELY do you want to use End... 99% of the time you see
someone using End I would contend that it is probably an inappropriate use.

If you need to halt all execution then you probably want to convert your sub
into a function of some sort that returns a value which indicates to a
calling procedure whether the calling procedure should continue.

Purely as an aside it is a good coding practice to minimize your usage of
Exit Sub. The difficulty is that if it is used heavily it can be difficult to
debug your procedures. If you can exit your sub at a number of different
points then it can be difficult to determine how much of your sub executed
before it exited. I normally will only use Exit Sub instructions at the
beginning of my procedures to determine if I want to execute the procedure at
all. Once I get into the body of the procedure I tend to use If-Then-Else
instructions to control the execution...

As always these are rules and rules are made to be broken. But before you
break any rules you need to understand why the rule exists...
--
HTH...

Jim Thomlinson


"clara" wrote:

Hi all,

what is the difference between End and Exit Sub if they are used in a Sub?
What state will be in after using them each?

Clara


--
thank you so much for your help

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 252
Default End and Exit Sub

Thank you so much Jim,

Why did you say "End halts all execution ".Because after that, Excel still
response to user's event. I mean application's execution does not stop yet.

Clara
thank you so much for your help


"Jim Thomlinson" wrote:

Exit sub does exactly what it says... it exits the current sub. If the sub
was called by another sub then that sub will continue on as normal. Exit sub
does NOT halt all execution. It just exit the current sub.

End halts ALL exectution. While this sounds tempting to do it also clears
all global and static variables. It is the equivalent of stopping your car by
steering it into a tree. Your car will stop but the consequences are
significant. VERY RARELY do you want to use End... 99% of the time you see
someone using End I would contend that it is probably an inappropriate use.

If you need to halt all execution then you probably want to convert your sub
into a function of some sort that returns a value which indicates to a
calling procedure whether the calling procedure should continue.

Purely as an aside it is a good coding practice to minimize your usage of
Exit Sub. The difficulty is that if it is used heavily it can be difficult to
debug your procedures. If you can exit your sub at a number of different
points then it can be difficult to determine how much of your sub executed
before it exited. I normally will only use Exit Sub instructions at the
beginning of my procedures to determine if I want to execute the procedure at
all. Once I get into the body of the procedure I tend to use If-Then-Else
instructions to control the execution...

As always these are rules and rules are made to be broken. But before you
break any rules you need to understand why the rule exists...
--
HTH...

Jim Thomlinson


"clara" wrote:

Hi all,

what is the difference between End and Exit Sub if they are used in a Sub?
What state will be in after using them each?

Clara


--
thank you so much for your help

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default End and Exit Sub

End stops the currently exectuing code dead in it's tracks inclucing
unloading all of the memory (from the heap and the stack). Code exectution
can be reinitiated but all of the global and static variabe values will be
gone...
--
HTH...

Jim Thomlinson


"clara" wrote:

Thank you so much Jim,

Why did you say "End halts all execution ".Because after that, Excel still
response to user's event. I mean application's execution does not stop yet.

Clara
thank you so much for your help


"Jim Thomlinson" wrote:

Exit sub does exactly what it says... it exits the current sub. If the sub
was called by another sub then that sub will continue on as normal. Exit sub
does NOT halt all execution. It just exit the current sub.

End halts ALL exectution. While this sounds tempting to do it also clears
all global and static variables. It is the equivalent of stopping your car by
steering it into a tree. Your car will stop but the consequences are
significant. VERY RARELY do you want to use End... 99% of the time you see
someone using End I would contend that it is probably an inappropriate use.

If you need to halt all execution then you probably want to convert your sub
into a function of some sort that returns a value which indicates to a
calling procedure whether the calling procedure should continue.

Purely as an aside it is a good coding practice to minimize your usage of
Exit Sub. The difficulty is that if it is used heavily it can be difficult to
debug your procedures. If you can exit your sub at a number of different
points then it can be difficult to determine how much of your sub executed
before it exited. I normally will only use Exit Sub instructions at the
beginning of my procedures to determine if I want to execute the procedure at
all. Once I get into the body of the procedure I tend to use If-Then-Else
instructions to control the execution...

As always these are rules and rules are made to be broken. But before you
break any rules you need to understand why the rule exists...
--
HTH...

Jim Thomlinson


"clara" wrote:

Hi all,

what is the difference between End and Exit Sub if they are used in a Sub?
What state will be in after using them each?

Clara


--
thank you so much for your help

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 RobcPettit[_2_] Excel Programming 3 October 23rd 06 06:03 PM
If Then Else with Exit For [email protected] Excel Programming 9 December 21st 05 04:27 PM
Help with For Next and Exit For Jacqui Excel Programming 6 November 3rd 05 11:17 AM
exit sub mike allen[_2_] Excel Programming 4 August 6th 04 05:39 PM
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 04:39 PM.

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

About Us

"It's about Microsoft Excel"