exiting a sub within a sub

tealeaf

Board Regular
Joined
May 15, 2002
Messages
160
hey
i have a module which calls another one. let's say
Sub A()
call B
end sub

sub B()
exit sub
end sub

i want to be able to exit both B and A sub. is that possible?

thanks in advance
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
With the subs you have listed, sub A will run and close and so will sub B. If you have a UserForm that is called up within sub A, I believe it will still be showing until sub B is done. If your specific issue is having the UserForm disappear, you may want to use an Unload UserForm command just before the Call statement.

Did this help?
 
Upvote 0
hi thanks!
in fact there is code in sub A after the call of sub B so it's more like:
sub A()
call B
....(code)....
end sub
sub B
...(code)...
exit sub
end sub

i don't want the code after the call B to be executed.
is that possible?
 
Upvote 0
The mystery is why you would even write code after the Call B in Sub A. You will probably need to qualify your code within Sub A so that the code knows whether to Call Sub B or to continue with Sub A. You will probably want to do something like this:

If (WHATEVER YOUR CONDITION IS) Then
Call Sub B
Exit Sub
End If

Does that help?
 
Upvote 0
Hi Tealeaf,

How about passing a variable from Sub B to Sub A (obviously placed before the Exit Sub line!) and then using an If..Then test in Sub A such that if the variable is True then Exit Sub?

Eg Dim fCloseTest as Boolean (Module level variable)

Then in Sub B use fCloseTest=True and,
in Sub A If fCloseTest=True Then Exit Sub
(set the variable to False at the start of Sub A).

Would this work for you?
 
Upvote 0
On 2002-08-29 08:35, Juan Pablo G. wrote:
The drastic solution would be to use End instead of Exit Sub

in fact this is the solution to my problem
i just want to thank everyone who helped. i chose the less elegant solution i think...but it works and it's fast. so i'm glad.
thanks again
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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