Ed said:
because
And if I've used module-level variables across both routines - which I
usually do - then I could wind up somewhere unexpected with an incorrect
variable value. Now some of my head-scratching moments are starting to
clear up!
The answer, then, is what I think Jonathan and Karl are trying patiently
to
explain - break routines and subroutines up into self-contained chunks
that
can be fully terminated before moving into the next chunk? Am I getting
warm?
That is much better!
Follow these general rules for your routines
1. Keep them relatively short
2. Have each routine perform a single task.
3. Give it a meaningful name
4. Keep variables local wherever possible, and give them meaningful names
5. Where data needs to be returned to the calling routine, pass it back as
the return value of a function wherever possible
6. Where possible use structured branching (i.e. If-Then, Select Case,
For-Next, Do-Loop etc) in preference to Goto. (This doesn't mean you should
*never* use Goto, but if you have a good design you shouldn't need it all
that often)
7. If you need to drop out of a loop early, use the Exit command (Exit Do,
Exit Do etc)
8. Thoroughly test each individual routine with the full range of data that
it might receive, before going on to the next.
More information can be found in these articles
The art of defensive programming
http://www.word.mvps.org/FAQs/MacrosVBA/MaintainableCode.htm
How to cut out repetition and write much less code, by using subroutines and
functions that take arguments
http://www.word.mvps.org/FAQs/MacrosVBA/ProcArguments.htm
Why variables should be declared properly
http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm
Also, I would strongly recommend buying a book called "Code Complete" by
Steve McConnell. It describes these principles much better than I can.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition
www.classicvb.org