Solved

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

Posted on 2011-09-09
8
266 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
 
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
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.

 
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

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

919 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

13 Experts available now in Live!

Get 1:1 Help Now