Solved

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

Posted on 2013-01-29
4
2,063 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
excel formula if this do this otherwise do this 2 16
Excel lookup tables & values 7 24
Excel Formula to split product code 5 13
VLOOKUP 6 15
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

813 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

10 Experts available now in Live!

Get 1:1 Help Now