Statement to exit root sub

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Code:
sub test1()
 
Call test2
 
End sub
 
Sub test2()
 
'Statement to exit both subs, test1 and test2
 
End sub

Is there any such thing? Maybe a statement like "On Error" that's actually "On Exit" instead?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I will check on that. These procedures are in a userform, will that end the breaoder userform execution as well?
 
Upvote 0
I don't want to lose several static variables, so I would use "Stop" if I wanted to halt everything (per VBA help).

However, I actually need to keep the userform running. Any other ideas? Is there any way to specifically target a sub to say, if this resumes then exit the sub? A user-defined event or something?

Edit: I realize I could just use a random variable, set it to 1 before the second procedure exits, then have an if-block that triggers if the variable is not empty to exit the other sub. But I asked this question to see if there was a cleaner way.
 
Upvote 0
One method for exiting the root sub without stopping everything is to return a value from the second routine which signals to the root sub that it's to exit. However to do that the called routine must be a function:-
Code:
sub test1()
 
  If test2 Then Exit Sub
 
End sub
 
Function test2() As Boolean
 
  test2 = true
 
End sub

Alternatively you can have a variable which you set in the called sub to signal to the root sub that it's to exit:-
Code:
DIM ExitFlag as Boolean
 
sub test1()
 
  ExitFlag=False
  Call test2
  If ExitFlag=True Then Exit Sub
 
End sub
 
Sub test2()
 
  ExitFlag=True
 
End sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,713
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top