View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
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