Exit-quit sub

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

Hello,

if I have many sub like this:

Sub macro1()
....
macro2 (calling macro2)
....
End sub()

Sub macro2()
....
exit-quit ???
....
End sub()

And in macro2 I need quit-exit all subs, not only macro2 (like Exit sub can
do it), can you help me?

tom
 
I think you could turn macro2 into a function that returns either false or
true. You can let it true if you want to exit immediately. The exit is of
course done from macro1. If you get false, proceed in macro1

/Fredrik
 
You can use the End statement to terminate all running VBA code.
Note that this will reset all global variables to their default
values.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Another couple of ways:

'a nice module level boolean variable
Dim OkToContinue as boolean
sub macro1()
call macro2
if oktocontinue =false then
exit sub
end if
end sub
sub macro2
'do stuff
if something = true then
oktocontinue = false
else
oktocontinue = true
end if
end sub

Or you can convert your subroutines to functions and return a boolean value.

sub macro1
if func2 = false then
exit sub
end if
end sub

function func2 as boolean
'do stuff
if something = true then
func2 = false
else
func2 = true
end if
end function
 

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

Similar Threads


Back
Top