Solved

How to exit the calling procedure when the user cancels a sub-procedure

Posted on 2011-09-09
8
277 Views
Last Modified: 2012-06-27
I am trying to find a way to exit the calling procedure when the user cancels a sub-procedure (a procedure called from within another procedure);
MyTestSub()

Dim Vars ' declare variables
     
On Error GoTo MyErrorHandler

Call MyFirstSub
     ' In this sub I prompt the user to cancel if a certain condition is true
     ' Answer = MsgBox("Do you want to continue ?", vbOKCancel, "Multiple Currency Types Selected")
     ' If I cancel here it goes on to the next sub - MySecondSub
     ' I want to have it exit MyTestSub
 

Call MySecondSub

Call OtherSubs

 
 MsgBox "Processing Complete for Business Report"

Exit Sub

MyErrorHandler:
  MsgBox Err.Description, _
    vbExclamation + vbOKCancel, _
    "Error: " & CStr(Err.Number)
   
End Sub
0
Comment
Question by:Ed_CLP
  • 4
  • 3
8 Comments
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 250 total points
ID: 36513669
There are various approaches to this issue, including raising custom errors and handling them, but I prefer simply to make MySecondSub a Function returning a Boolean value - True if it succeeds, and false if the user cancelled.

Then you can use code like

     If Not MySecondSub() Then Exit Sub

Open in new window

0
 
LVL 33

Expert Comment

by:Norie
ID: 36514245
You can end all code execution with End.
0
 
LVL 41

Expert Comment

by:dlmille
ID: 36515170
not for points...

I follow @andrewssd3 style.    It would be:

if not myFirstSub() then exit sub

Dave
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 17

Expert Comment

by:andrewssd3
ID: 36515829
Yes - just saying End is a bit brutal - you would usually need to do some tidying up, destroy objects, etc.
0
 
LVL 33

Expert Comment

by:Norie
ID: 36515907
You could say turning a sub into a function is a bit much.

I suppose there's also the option of a public variable.

I'm sure there's other methods too.:)
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36516051
Agreed - It's really just a matter of preference.  I've got in the habit of making all called procedures Functions because it does generally make life easier, and you almost always need to know whether a called subroutine has run successfully.  I know people who like to raise custom errors for everything and have elaborate error handling.
0
 
LVL 33

Expert Comment

by:Norie
ID: 36516064
Can't say I've not done that but not very often.

Error handling  in VBA...mmmm.:)
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36516435
inmorie - my feelings exactly - best avoided!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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…
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 in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

772 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