Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default Qutting VBA code in case of error


Hi All,

I have a procedure A which calls a procedure B. Now, in case an happens
an error happens in Procedure B I want to exit the whole VBA (I want
even the Produre A should stop running).

If I simply put On error Exit Sub in Procedure B, the program exists B
and comes back to A and runs the other statements in A.

In short is there something like Exit Sub which applies to the all the
Procedures in code. I tried Application.Quit which closes excel
Alltogether (which is not right)

Help is appreciated.

Thanks a lot
Ankur
www.xlmacros.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Qutting VBA code in case of error

There is "End", but read the help on this before you use it. I cannot think
of a reason to use it in any code you want remotely reliable.

A better way is to make your ProcB a function and check its return value in
ProcA:
<Return Values
Sub ProcA_Return()
If ProcB_Return = False Then Exit Sub
'Other code
End Sub

Function ProcB_Return() As Boolean 'Or Long
'Deal with errors created in code
On Error GoTo Handler

'Deal with some incorrect entries
If Range("A1").Value < 10 Then
ProcB_Return = False
Exit Function
End If

'Other code
'All OK
ProcB_Return = True

Exit Function
Handler:
ProcB_Return = False

End Function
</Return Values

There are also ways of using Err.Raise allowing the unhandled error to
bubble up to the calling routine, which deals with it:
<Raise Errors
Sub ProcA_Errors()
'Error handling in to the "top" Proc
On Error GoTo Handler

Call ProcB_Errors
'Other code

Exit Sub
Handler:
'Deal with the error I you can/want
MsgBox Err.Source & vbNewLine & Err.Description
End Sub

Sub ProcB_Errors()
'No error handling here

'Deal with some incorrect entries
If Range("A1").Value < 10 Then
Err.Raise 10010, "ProcB_Errors", "What is wrong"
End If

'Cause an error
MsgBox 1 / 0

End Sub
</Raise Errors

NickHK

"ankur" wrote in message
oups.com...

Hi All,

I have a procedure A which calls a procedure B. Now, in case an happens
an error happens in Procedure B I want to exit the whole VBA (I want
even the Produre A should stop running).

If I simply put On error Exit Sub in Procedure B, the program exists B
and comes back to A and runs the other statements in A.

In short is there something like Exit Sub which applies to the all the
Procedures in code. I tried Application.Quit which closes excel
Alltogether (which is not right)

Help is appreciated.

Thanks a lot
Ankur
www.xlmacros.com



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default Qutting VBA code in case of error

Thanks a lot
Perfect...

Regards
Ankur

On Jan 24, 12:44 pm, "NickHK" wrote:
There is "End", but read the help on this before you use it. I cannot think
of a reason to use it in any code you want remotely reliable.

A better way is to make your ProcB a function and check its return value in
ProcA:
<Return Values
Sub ProcA_Return()
If ProcB_Return = False Then Exit Sub
'Other code
End Sub

Function ProcB_Return() As Boolean 'Or Long
'Deal with errors created in code
On Error GoTo Handler

'Deal with some incorrect entries
If Range("A1").Value < 10 Then
ProcB_Return = False
Exit Function
End If

'Other code
'All OK
ProcB_Return = True

Exit Function
Handler:
ProcB_Return = False

End Function
</Return Values

There are also ways of using Err.Raise allowing the unhandled error to
bubble up to the calling routine, which deals with it:
<Raise Errors
Sub ProcA_Errors()
'Error handling in to the "top" Proc
On Error GoTo Handler

Call ProcB_Errors
'Other code

Exit Sub
Handler:
'Deal with the error I you can/want
MsgBox Err.Source & vbNewLine & Err.Description
End Sub

Sub ProcB_Errors()
'No error handling here

'Deal with some incorrect entries
If Range("A1").Value < 10 Then
Err.Raise 10010, "ProcB_Errors", "What is wrong"
End If

'Cause an error
MsgBox 1 / 0

End Sub
</Raise Errors

NickHK

"ankur" wrote in ooglegroups.com...





Hi All,


I have a procedure A which calls a procedure B. Now, in case an happens
an error happens in Procedure B I want to exit the whole VBA (I want
even the Produre A should stop running).


If I simply put On error Exit Sub in Procedure B, the program exists B
and comes back to A and runs the other statements in A.


In short is there something like Exit Sub which applies to the all the
Procedures in code. I tried Application.Quit which closes excel
Alltogether (which is not right)


Help is appreciated.


Thanks a lot
Ankur
www.xlmacros.com- Hide quoted text -- Show quoted text -


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
Case without Select Case error problem Ayo Excel Discussion (Misc queries) 2 May 16th 08 03:48 PM
VBA Proper Case Code EMoe[_3_] Excel Programming 4 May 25th 05 08:07 PM
Error in code re case construction PFL Excel Programming 2 February 6th 05 08:26 PM
2 x Case // Error why ? Philipp Oberleitner Excel Programming 7 June 26th 04 02:20 PM
Proper case code not working right Juan Excel Programming 3 April 16th 04 11:11 PM


All times are GMT +1. The time now is 09:31 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"