How To Quit Subroutine from a called subroutine

R

Rich J

I have a subroutine that opens a form which calls other routines. Is there a
simple way to quit all and stop running everything from the called routine?

When the cursor returns to the calling routine I don't want the rest of that
code to run in some situations.
I can add a test right after it returns but thought there might be a
definitive command.

Thanks
 
T

Tom Ogilvy

no good definitive command. You can use END, but it clears all global
variables and does not clean up. Most recommend against using it.
 
R

Rich J

That is actually exactly the command I needed. It is in a Set Up routine and
if certain dates have not been entered, Set Up needs to stop running and a
calendar sheet left open for amending. The macro would keep running and
close the sheet though without the necessary changes. Now, after making the
changes, the user just pushes the Set Up button again and won't have to go to
that part of the program again. It couldn't be a more simple solution.

Thank you once again Tom.
 
S

Simon Lloyd

Rich said:
I have a subroutine that opens a form which calls other routines. Is
there a
simple way to quit all and stop running everything from the called
routine?

When the cursor returns to the calling routine I don't want the rest of
that
code to run in some situations.
I can add a test right after it returns but thought there might be a
definitive command.

ThanksThe simple solution is to add a message box as VbOkCancel or VbYesNo you
would use it in this fashion

Code:
 
J

Jon Peltier

What I often do is change the sub into a function:

Function MySub() As Boolean

If the function finishes successfully, I assign it a value of True;
otherwise the function returns False. Then in the calling sub I test the
value of the function, and if False, I can exit gracefully from this sub as
well:

Sub Main()

' blah

bTest = MySub
If Not bTest then
' Bail Out
GoTo ExitSub
End If

' yada

ExitSub:

' clean up

End Sub

- Jon
 
R

Rich J

Thank you both for additional suggestions. The routine is for automatically
transferring all the worksheets (sometimes over 100) created in an older
version of a workbook I developed to the updated workbook it is running from.
I have tried to keep the program itself fairly intuitive for users. I put in
several interview questions during Set UP in msgbox's to make sure that the
new workbook will have the correct calendar it needs. Stopping the Set UP
cold if they haven't already made sure that it is correct is exactly what I
needed. It closes the Set UP form and leaves the calendar sheet open for
amending. If the calendar is wrong in the older workbook (which happens more
often than I like to hear) then each sheet is automatically corrected if the
calendar is correct in the new workbook.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top