Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default When To use GOTO Sub?


I sometimes see people seperating their code into many differen
subroutines then joining it together with goto ____. What is th
purpose of this? Will it slow down or make the macro faster? or is i
used just for organization purposes

--
MC8
-----------------------------------------------------------------------
MC82's Profile: http://www.excelforum.com/member.php...fo&userid=1868
View this thread: http://www.excelforum.com/showthread.php?threadid=54008

  #2   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default When To use GOTO Sub?

IMO it should be used as little as possible. I think it makes the code hard
to read. VBA has plenty of loops and condition statements that make
excessive use of Goto unnecessary.

The only time I use it is to set up an ErrorHandler or CleanUp procedure,
which can not only trap an error and decide what to do (resume or exit or
something else), but can also reset any Application settings the macro may
have changed, such as setting Calc to manual. If an error occurs somewhere
in the middle of the code, I usually want the calc set back to automatic
before exiting the procedure.

On Error GoTo ErrorHandler








"MC82" wrote:


I sometimes see people seperating their code into many different
subroutines then joining it together with goto ____. What is the
purpose of this? Will it slow down or make the macro faster? or is it
used just for organization purposes?


--
MC82
------------------------------------------------------------------------
MC82's Profile: http://www.excelforum.com/member.php...o&userid=18682
View this thread: http://www.excelforum.com/showthread...hreadid=540083


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default When To use GOTO Sub?

As a general rule, you should use Goto only with On Error. While
using Goto is theoretically faster than calling a sub, it leads
quickly to code that is difficult to follow and maintain. It
leads to what is called "spaghetti code" where you GOTO one label
then GOTO another then another. If you need a Goto, you can
almost always use a separate procedure instead. This makes the
code much simpler to maintain.

In summary, don't use Goto.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"MC82" wrote
in message
...

I sometimes see people seperating their code into many
different
subroutines then joining it together with goto ____. What is
the
purpose of this? Will it slow down or make the macro faster?
or is it
used just for organization purposes?


--
MC82
------------------------------------------------------------------------
MC82's Profile:
http://www.excelforum.com/member.php...o&userid=18682
View this thread:
http://www.excelforum.com/showthread...hreadid=540083



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default When To use GOTO Sub?


Thanks for the explanation guys. What about the usage of "Call" Sub?
Is this also something I should refrain from using much

--
MC8
-----------------------------------------------------------------------
MC82's Profile: http://www.excelforum.com/member.php...fo&userid=1868
View this thread: http://www.excelforum.com/showthread.php?threadid=54008

  #5   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default When To use GOTO Sub?

Depends. It is particularly useful to set up a separate sub that can be
called by other subs if those other subs need to perform similar functions.
If you have to ever make changes to the code, it is easier because you only
have to do it once.

For example, I have one template that has to perform a dozen or so different
filter operations on a particular table. Instead of writing 12 filter
procedures, I have one sub that actually performs the filtering. The other
12 pass parameters to it (filter criteria, etc) and perform any other
procedures/setup that is specific to their own filter operation (one filter
operation has to get user input, so it does that and passes the info along to
the filter sub - but getting user input is specific only to that particular
filter operation).

I usually go with what appears organized and logical to me. Using
additional subs can also help w/testing and debugging. You can set up some
dummy data and a test macro that calls the sub you want to test/debug and
step through it without having to run/step through one giant macro in order
to test/debug only one particular part of it. OTOH I avoid setting up
another sub when it would require too much duplicate work, or a lot of
parameter passing, or require most of my variables to be declared at the
module level.



"MC82" wrote:


Thanks for the explanation guys. What about the usage of "Call" Sub?
Is this also something I should refrain from using much?


--
MC82
------------------------------------------------------------------------
MC82's Profile: http://www.excelforum.com/member.php...o&userid=18682
View this thread: http://www.excelforum.com/showthread...hreadid=540083


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
? IF & GOTO ? Karen D Excel Worksheet Functions 17 September 2nd 09 10:58 PM
If.....Then GoTo....... Alec H Excel Discussion (Misc queries) 4 February 22nd 06 02:42 PM
On Error Goto doesn't goto Paul Excel Programming 1 October 15th 04 03:51 PM
On Error Goto doesn't goto Paul Excel Programming 0 October 15th 04 03:05 PM
Goto next used row JonWayn Excel Programming 0 November 20th 03 03:00 AM


All times are GMT +1. The time now is 04:18 PM.

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"