Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default exit all subs? (from embedded sub)

I have a button on a sheet that calls a sub, and that sub calls other subs,
I might even have a third-level sub in there somewhere, and each of those
subs have plenty of code.

I just found out that there is a potential problem with files that are
auto-loaded behind the scenes; now I need to pop a messagebox to the user
with a few key pieces of info to confirm whether the source files are valid.
Unfortunately, the best place to do that is within an embedded sub.

So while I've used exit for and exit sub before, my understanding is that
they each only exit the current "level". If I exit this embedded sub, then
I'll still have the main calling sub run, which I don't want (because it
will try to run the bad data).

What is the appropriate way to stop or reset the processing of any/all VBA
code from within an embedded sub? it could be a serious re-write to try to
set a variable in multiple places to skip each chunk of code, so I'd prefer
to just stop the code altogether and let the user upload an appropriate
file, then restart.

Thanks for any help,
Keith

--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default exit all subs? (from embedded sub)

Hi,

Try End (instead of Exit Sub).

Regards,

Chris.

--
Chris Marlow
MCSD.NET, Microsoft Office XP Master


"KR" wrote:

I have a button on a sheet that calls a sub, and that sub calls other subs,
I might even have a third-level sub in there somewhere, and each of those
subs have plenty of code.

I just found out that there is a potential problem with files that are
auto-loaded behind the scenes; now I need to pop a messagebox to the user
with a few key pieces of info to confirm whether the source files are valid.
Unfortunately, the best place to do that is within an embedded sub.

So while I've used exit for and exit sub before, my understanding is that
they each only exit the current "level". If I exit this embedded sub, then
I'll still have the main calling sub run, which I don't want (because it
will try to run the bad data).

What is the appropriate way to stop or reset the processing of any/all VBA
code from within an embedded sub? it could be a serious re-write to try to
set a variable in multiple places to skip each chunk of code, so I'd prefer
to just stop the code altogether and let the user upload an appropriate
file, then restart.

Thanks for any help,
Keith

--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default exit all subs? (from embedded sub)

Pass back a return value to the caller, and if a certain value exit that
also

Call Sub2(rtn)
If rtn = -1 Then
Exit Sub
...

Sub Sub2(ByRef nReturn as Long)
... do some stuff

Call Sub3(nReturn)
If nReturn = -1 Then
Exit Sub
...

End Sub

Sub Sub3(ByRef nReturn as Long)
... do some stuff
If some error Then
nReturn = -1
Exit Sub
...

End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"KR" wrote in message
...
I have a button on a sheet that calls a sub, and that sub calls other

subs,
I might even have a third-level sub in there somewhere, and each of those
subs have plenty of code.

I just found out that there is a potential problem with files that are
auto-loaded behind the scenes; now I need to pop a messagebox to the user
with a few key pieces of info to confirm whether the source files are

valid.
Unfortunately, the best place to do that is within an embedded sub.

So while I've used exit for and exit sub before, my understanding is that
they each only exit the current "level". If I exit this embedded sub, then
I'll still have the main calling sub run, which I don't want (because it
will try to run the bad data).

What is the appropriate way to stop or reset the processing of any/all VBA
code from within an embedded sub? it could be a serious re-write to try to
set a variable in multiple places to skip each chunk of code, so I'd

prefer
to just stop the code altogether and let the user upload an appropriate
file, then restart.

Thanks for any help,
Keith

--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default exit all subs? (from embedded sub)

If you have static or public variables, be aware that "End" will reinitialize
them.

"Chris Marlow" wrote:

Hi,

Try End (instead of Exit Sub).

Regards,

Chris.

--
Chris Marlow
MCSD.NET, Microsoft Office XP Master


"KR" wrote:

I have a button on a sheet that calls a sub, and that sub calls other subs,
I might even have a third-level sub in there somewhere, and each of those
subs have plenty of code.

I just found out that there is a potential problem with files that are
auto-loaded behind the scenes; now I need to pop a messagebox to the user
with a few key pieces of info to confirm whether the source files are valid.
Unfortunately, the best place to do that is within an embedded sub.

So while I've used exit for and exit sub before, my understanding is that
they each only exit the current "level". If I exit this embedded sub, then
I'll still have the main calling sub run, which I don't want (because it
will try to run the bad data).

What is the appropriate way to stop or reset the processing of any/all VBA
code from within an embedded sub? it could be a serious re-write to try to
set a variable in multiple places to skip each chunk of code, so I'd prefer
to just stop the code altogether and let the user upload an appropriate
file, then restart.

Thanks for any help,
Keith

--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default exit all subs? (from embedded sub)


You might like to consider raising an error using eg err.raise 513 (513
is the start of the userdefined errors) and then you can trap this
however far up the call-stack you want to go.

It may give you a more controlled process

regards


--
tony h
------------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=530511

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining two Subs Petitboeuf Excel Discussion (Misc queries) 6 April 27th 06 03:42 PM
Sort subs Gary''s Student Excel Programming 3 October 23rd 05 09:35 PM
Scope of Subs? Ken Loomis Excel Programming 9 August 6th 05 07:36 PM
ending subs [email protected] Excel Programming 2 May 17th 05 06:13 PM
If a called sub exit, how to the caller exit right away? luvgreen[_4_] Excel Programming 4 February 24th 04 05:06 PM


All times are GMT +1. The time now is 11:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"