Solved

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

Posted on 2013-01-29
4
1,956 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel - count records based name and based on null and not null 5 31
What is format f12.8 for a CSV file 6 40
increment numbers by 10 11 29
Excel sheet question 12 22
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 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

23 Experts available now in Live!

Get 1:1 Help Now