Conditionally exit a subroutine

I have a button in my excel workbook that launches nine macros/routines.  The macros run one after the other and are dependent on the data generated by one or more of the previous.  Of course when I started i only ran three or so macros and the whole thing crept/grew to where I am now.  Problem I have is that I want to exit out of the routines if a error condition is found (I have a variable, error count, and if this equalls one or more the condition is met).  So say following the execution of macro #3 I run my error check and If if finds an error it will bail on the remaining macros.  

Alternately i can embed my macro into macro #3 and do the same bailing out.  

Still another option is to merge all the macros  into one , embed the error check code, and bail out if an error is found.  Bottom line is I don't know how to do this, nor how best to do this.
deskchainsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
To exit a subroutine, use Exit Sub

To do that conditionally, test for the condition using IF THEN, then have Exit Sub as the THEN part.
aikimarkCommented:
If you want to chain them, then create a new routine that will invoke the other nine in succession.  After each routine ends, you check to see if they raised an error and act appropriately.
Martin LissOlder than dirtCommented:
Option Explicit
Private bError As Boolean

Sub1() ' and similarly for Sub2 through 9
If bError Then
    Exit Sub
End If

' body of your sub

If <some error occurs> Then
    bError = True
End If

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deskchainsAuthor Commented:
I used a similar approach, but made the bError variable a global so that I could reference it multiple times for the same test.  not sure if i needed to, but that is what i did.
Martin LissOlder than dirtCommented:
I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.