Solved

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

Posted on 2013-01-29
4
2,590 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 93

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

626 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