Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exit Main Sub from within Internal Sun 1

Status
Not open for further replies.

RobynC

Aerospace
Jul 23, 2007
13
0
0
GB
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?
 
Replies continue below

Recommended for you

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
 
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)
 
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.
 
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
 
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
 
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).
 
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.
 
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
 
Status
Not open for further replies.
Back
Top