Solved

Exit Main Sub from a "Called" procedure: -  Excel VBA

Posted on 2013-01-29
4
1,866 Views
Last Modified: 2013-01-29
Dear Experts

I am running a sub that calls other procedures, rather than have one long main sub.
One of the subs called ends with a msgbox with vbOKCancel as its buttons.

vbOK is fine; that just continues the main procedure.

How do i get the main procedure to exit if the msgbox from the called routine is vbCancel?

Please see attached workbook example.
Query-ExitSub.xlsm
0
Comment
Question by:David Phelops
  • 2
4 Comments
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 total points
ID: 38831304
Generally, instead of making the subsidiary routines subs, make them functions that return a value to the calling sub that allows for a test of whether to continue.

For example, if function Foo returns a boolean...


    If Foo(SomeArgumentHere) = False Then
        MsgBox "Oh my, time to stop!", vbExclamation
        Exit Sub
    End If

Open in new window

0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 400 total points
ID: 38831352
There are two ways to do this

The first way is easy but not good if global variables are used. Use this in the called sub

        mbr = MsgBox("An unknown error.....", vbOKCancel)
        If mbr = vbCancel Then End


The second way is to return the mbr variable to the main (calling) sub and then exit if mbr=vbCancel
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38831375
Example of the second method

Sub Main()
.
.
calledsub(mbr)
if mbr=vbCancel then exit sub
.
.
end sub


sub calledsub(mbr)
.
.
mbr=msgbox("message",vbOKCancel)
if mbr=vbCancel then exit sub
.
.
end sub
0
 

Author Closing Comment

by:David Phelops
ID: 38831456
@ssaqibh
Thank you - fortunately, I have no global variables in this routine, so method #1 is fine for now.
Method #2 i shall keep in reserve!
Cheers - I like simple, elegant solutions!

I should like to give @matthewspatrick some credit for suggesting the function route, which i'm sure with more time and application, i can implement
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now