Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2007

Tip: Looking for answers? Try searching our database.

Qutting VBA code in case of error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ankur - 24 Jan 2007 07:18 GMT
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
NickHK - 24 Jan 2007 07:44 GMT
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

> Hi All,
>
[quoted text clipped - 14 lines]
> Ankur
> www.xlmacros.com
ankur - 24 Jan 2007 08:37 GMT
Thanks a lot
Perfect...

Regards
Ankur

> 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.
[quoted text clipped - 78 lines]
> > Ankur
> >www.xlmacros.com- Hide quoted text -- Show quoted text -
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.