Smart questions
Smart answers
Smart people
Join Eng-Tips Forums
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Eng-Tips
*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

RobynC (Aerospace) (OP)
16 Aug 07 9:42
Hi, I'm quite new to VBA.

I have a main sub which calls other subs in order to run.  If a condition in one of these Subs is met I want to exit the main sub.  Using 'Exit Sub' just exits the internal sub rather than the whole program.  I remember seeing how to do this somewhere a while back but can't remember where and can not find it again now.  So my question is how do I do this?
RobynC (Aerospace) (OP)
16 Aug 07 9:47
Just to clarify when I say Main Sub I don't mean one called 'Sub Main()' I just mean that its a Sub that does little more than call the other subs in the correct order, so that the code is nicely split up into easily understandable blocks
Helpful Member!  prost (Structural)
16 Aug 07 10:19
My preference would be to create an error code system in your main subroutine and the subordinate subroutines and functions. Say at the top, above the main subroutine, type in Dim IERROR as Integer (that way IERROR is passed around like IERROR was in a common block), then you can set up a short error code system that you can use to control program execution. Say in your subordinate subroutine, you want the whole program to stop when condition X is met. if condition X is met, then set  IERROR=1, and Exit Sub. When you get back to the line in the main subroutine where this subordinate subroutine is called, you can have an IF statement that IF IERROR=1 then Exit Sub. That should stop it.

or if you don't want to do it that way, just set IERROR=0 right before the call to the subordinate subroutine, pass IERROR to this subordinate subroutine, then when condition X is met, set IERROR=1, Exit Sub,  then have the If statement that stops the main sub when IERROR=1. (the reason I like to use a system of error codes is I like to print out when the program stopped, and why it stopped)
RobynC (Aerospace) (OP)
16 Aug 07 10:27
Thanks prost, that is very helpful, and thanks for explaining it so well.  I'm just learning VBA so replys that explain why you would want to do something as well as how to do it are greatly appreciated.
handleman (Automotive)
16 Aug 07 10:41
One way would be to use the "End" statement.  End will just stop execution wherever it is.  However, I don't think that's a very clean way to do it.  You can use Prost's suggestion, or you can change the called sub into a function that returns a boolean true/false.  Declaring a function is pretty similar to declaring a sub, and to return a value from a function you just set that function's name to your desired value inside the function.  So if you currently have

CODE

Sub Main()

    Call MySub1
    Call MySub2
    Call MySub3

End Sub

Sub MySub1()
    'Do some stuff
End Sub

Sub MySub2()
    'Do some different stuff
    'where you want to quit
    'the program altogether
    'depending on conditions
End Sub

Sub MySub3()
    'Do some stuff that you don't
    'want to do if something
    'happens in MySub2
End Sub

you would change to something like

CODE

Sub NewMain()

    Call MySub1
    If MyFxn2 Then '<-This line actually calls MyFxn2 and returns True or False
        Call MySub3
        'and however much other stuff you want to do
    End If

End Sub

Sub MySub1()
    'Do some stuff
End Sub

Function MyFxn2() As Boolean
    'Do some different stuff
    'where you want to quit
    'the program altogether
    'depending on conditions
    If True Then '<-Here is your condition
        MyFxn2 = True 'Return a value of True
    Else
        MyFxn2 = False 'Return a value of False
    End If
End Function

Sub MySub3()
    'Do some stuff that you don't
    'want to do if MyFxn2
    'returns "False"
End Sub
RobynC (Aerospace) (OP)
16 Aug 07 11:06
Thanks, that's interesting, I hadn't thought of doing it like that.  So you can pretty much write anything as a function then as long as it has a quantifiable outcome?  

I think for this program I will use prost's method as there would be a fair amount of code in the function's If and End If and I think the IERROR method will look neater.  But that has got me thinking of different ways to solve problems so thanks
handleman (Automotive)
16 Aug 07 11:33
Yes, you can pretty much write anything as a function as long as you can return some value or object.  And you can pass values or objects into functions.  Limiting yourself to subs requires lots of workarounds and makes your code much less modular (i.e. reusable).
RobynC (Aerospace) (OP)
16 Aug 07 12:15
OKay, I will try to do that.  At the moment I'm working on a fairly specialised program (automating some finite element analysis - A wonderful starter job!)  But will look into using functions more, since as at present I only use them for some simple calculations.
cummings54 (Chemical)
16 Aug 07 13:37
One possible approach is to raise user defined errors in the sub routines and the On Error stament in your main program to control execution.  In the sample code a error raised in sub two will cause myMain to exit.

CODE

Dim Routine As Integer
Sub myMain()
  Routine = 2
  On Error GoTo ExitMain
  Call one
  Call two
  Call three
ExitMain:
End Sub

Sub one()
  If Routine = 1 Then Err.Raise 513, "Sub One"
End Sub

Sub two()
  If Routine = 2 Then Err.Raise 514, "Sub Two"
End Sub

Sub three()
  If Routine = 3 Then Err.Raise 515, "Sub Three"
End Sub
ck1999 (Chemical)
6 Sep 07 22:58
Have you tried

End


ck1999

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!

Back To Forum

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close