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

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);

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

  MsgBox Err.Description, _
    vbExclamation + vbOKCancel, _
    "Error: " & CStr(Err.Number)
End Sub
Who is Participating?
andrewssd3Connect With a Mentor Commented:
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

NorieVBA ExpertCommented:
You can end all code execution with End.
not for points...

I follow @andrewssd3 style.    It would be:

if not myFirstSub() then exit sub

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Yes - just saying End is a bit brutal - you would usually need to do some tidying up, destroy objects, etc.
NorieVBA ExpertCommented:
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.:)
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.
NorieVBA ExpertCommented:
Can't say I've not done that but not very often.

Error handling  in VBA...mmmm.:)
inmorie - my feelings exactly - best avoided!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.