Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default exiting the whole macro



I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the whole
macro, currently end sub and exit sub just exit the second sub and continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 373
Default exiting the whole macro

Sub test()
If Test_2() = "exit now" Then Exit Sub
MsgBox ("fail")
End Sub

Function Test_2() As String
'the code I need has to go here!
Test_2 = "exit now"
End Function


"tigoda" wrote in message
...


I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the
whole
macro, currently end sub and exit sub just exit the second sub and
continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default exiting the whole macro

Exit Sub should work.

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
If myCriteria = True Then
Exit Sub
End If
'the code I need has to go here!
End Sub

I threw in an If statement to show that you can set the criteria for when
you want to exit the Test_2 sub.

The Exit Sub in Test_2 will automatically revert back to the calling sub
Test_1.
It will begin executing on the line following the call for Test_2, or in
this case,
your message box.


"tigoda" wrote:



I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the whole
macro, currently end sub and exit sub just exit the second sub and continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default exiting the whole macro

that works, but only if the exit sub is in the second sub, not in a 3rd or
4th,

i want this to be attached to some error handling, so calling the sub that
has the 'bomb out of everytihng' code could happen any time,

there will be a sub to deal with errors and to print meaning fill text boxes
to help the user with the error, this error sub will be either 2 or 3 subs in
and will be called from the on err bit,


"Zone" wrote:

Sub test()
If Test_2() = "exit now" Then Exit Sub
MsgBox ("fail")
End Sub

Function Test_2() As String
'the code I need has to go here!
Test_2 = "exit now"
End Function


"tigoda" wrote in message
...


I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the
whole
macro, currently end sub and exit sub just exit the second sub and
continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default exiting the whole macro

the problem being i dont want it to display the msgbox("fail") thats a test
to see if the macro ends totaly without going back to the calling sub

"JLGWhiz" wrote:

Exit Sub should work.

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
If myCriteria = True Then
Exit Sub
End If
'the code I need has to go here!
End Sub

I threw in an If statement to show that you can set the criteria for when
you want to exit the Test_2 sub.

The Exit Sub in Test_2 will automatically revert back to the calling sub
Test_1.
It will begin executing on the line following the call for Test_2, or in
this case,
your message box.


"tigoda" wrote:



I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the whole
macro, currently end sub and exit sub just exit the second sub and continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default exiting the whole macro

Sorry, I misunderstood your posting. You can use the End command. That
stops everything, but it wipes out all your variables. You can also use
Application.Quit, but that shuts down that instance of Excel. The only other
suggestion I have is to incorporate the procedure in Test_2 into Test_1 so
that you only have one sub and then use the Exit Sub command. Of course,
that may not be practical because of size.

"tigoda" wrote:

the problem being i dont want it to display the msgbox("fail") thats a test
to see if the macro ends totaly without going back to the calling sub

"JLGWhiz" wrote:

Exit Sub should work.

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
If myCriteria = True Then
Exit Sub
End If
'the code I need has to go here!
End Sub

I threw in an If statement to show that you can set the criteria for when
you want to exit the Test_2 sub.

The Exit Sub in Test_2 will automatically revert back to the calling sub
Test_1.
It will begin executing on the line following the call for Test_2, or in
this case,
your message box.


"tigoda" wrote:



I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the whole
macro, currently end sub and exit sub just exit the second sub and continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default exiting the whole macro

The END command by itself halts code execution. However, it doesn't do it
nicely.

(see help).

To do it nicely, you will need to put your error handling only in the
topmost routine and vba will seek the first error handler going back up the
call chain ending up in the topmost sub where you can exit.

--
Regards,
Tom Ogilvy


"tigoda" wrote:



I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the whole
macro, currently end sub and exit sub just exit the second sub and continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 373
Default exiting the whole macro

Maybe this will help. Notice that the error ripples back to the first
routine. James

Sub Sub1()
On Error GoTo ErrHandler
Sub2
MsgBox "No error found"
Exit Sub
ErrHandler:
MsgBox "Error " & Err & Chr(13) & Error
End Sub

Sub Sub2()
Sub3
End Sub

Sub Sub3()
Error 15
End Sub

"tigoda" wrote in message
...
that works, but only if the exit sub is in the second sub, not in a 3rd or
4th,

i want this to be attached to some error handling, so calling the sub that
has the 'bomb out of everytihng' code could happen any time,

there will be a sub to deal with errors and to print meaning fill text
boxes
to help the user with the error, this error sub will be either 2 or 3 subs
in
and will be called from the on err bit,


"Zone" wrote:

Sub test()
If Test_2() = "exit now" Then Exit Sub
MsgBox ("fail")
End Sub

Function Test_2() As String
'the code I need has to go here!
Test_2 = "exit now"
End Function


"tigoda" wrote in message
...


I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the
whole
macro, currently end sub and exit sub just exit the second sub and
continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting
to
ending and closing the whole sheet,

can anyone help?






  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default exiting the whole macro

thanks, the end works, i dont want any of the variables saved, if the error
occurs its because there is rubbish in the input file, so i want the whole
thing wiped

"JLGWhiz" wrote:

Sorry, I misunderstood your posting. You can use the End command. That
stops everything, but it wipes out all your variables. You can also use
Application.Quit, but that shuts down that instance of Excel. The only other
suggestion I have is to incorporate the procedure in Test_2 into Test_1 so
that you only have one sub and then use the Exit Sub command. Of course,
that may not be practical because of size.

"tigoda" wrote:

the problem being i dont want it to display the msgbox("fail") thats a test
to see if the macro ends totaly without going back to the calling sub

"JLGWhiz" wrote:

Exit Sub should work.

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
If myCriteria = True Then
Exit Sub
End If
'the code I need has to go here!
End Sub

I threw in an If statement to show that you can set the criteria for when
you want to exit the Test_2 sub.

The Exit Sub in Test_2 will automatically revert back to the calling sub
Test_1.
It will begin executing on the line following the call for Test_2, or in
this case,
your message box.


"tigoda" wrote:



I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the whole
macro, currently end sub and exit sub just exit the second sub and continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default exiting the whole macro

same to you tom, thanks

"Tom Ogilvy" wrote:

The END command by itself halts code execution. However, it doesn't do it
nicely.

(see help).

To do it nicely, you will need to put your error handling only in the
topmost routine and vba will seek the first error handler going back up the
call chain ending up in the topmost sub where you can exit.

--
Regards,
Tom Ogilvy


"tigoda" wrote:



I have the following example:

Sub test()
call Test_2
MsgBox ("fail")
End Sub

Sub Test_2()
'the code I need has to go here!
End Sub

in the second sub I need a command/section of code that will exit the whole
macro, currently end sub and exit sub just exit the second sub and continue
running the first sub (displaying the msg box "fail" as a test)

there has to be a way to safety bomb out of the macro without resorting to
ending and closing the whole sheet,

can anyone help?

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
Macro not exiting correctly Brad Excel Programming 4 June 18th 07 04:54 PM
Help with Exiting Sub if Value is Not Found Filo Excel Programming 5 May 23rd 07 03:14 PM
Exiting a Textbox EGL Excel Programming 2 May 13th 07 09:49 AM
Macro suffers from premature exiting Fred Smith Excel Programming 6 February 14th 07 01:57 AM
Exiting a VBA Macro by user intervention JON Excel Programming 1 April 23rd 04 06:01 PM


All times are GMT +1. The time now is 09:30 AM.

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

About Us

"It's about Microsoft Excel"