VBA Errors in Excel

E

esukkar

Hi all, I have code that runs, and has errors ignored - using an on
error resume next statement. However, after a while, I need it to again
check for errors - that is stop the resume next. For example


On error resume next
....
....code with errors ignored
....
.... 'need it to stop it ignoring errors at this point

....code where errors are not ignored
....
....
....'need it not to ignore errors here

this is probably a really simple problem, but I am new to vba.

Is there a way to break out of the error ignoring. Also, I cant run the
second part of the code first (before the ignore errors statement,
since it relies on that code running in the first place.

Thanks
Cedop
 
J

JE McGimpsey

While this isn't an Excel group (you should use

microsoft.public.excel.programming

), to stop error handling, use

On Error GoTo 0
 
N

Nick Hebb

Here's a sample that shows the different options:

Public Sub DoSomethingErrorProne()
On Error GoTo Err_Handler
' good code

On Error Resume Next
' error-prone code that contains objects

On Error GoTo Err_Handler
' good code

On Error GoTo 0
'error-prone code that doesn't contain objects

On Error GoTo Err_Handler
' good code

Exit_Handler:
' code to set any objects to Nothing
Exit Sub

Err_Handler:
MsgBox Err.Description 'or Err.Raise
Resume Exit_Handler

Exit Sub

So to resume catching errors, you will need to have an error handler
that you can re-direct the errors to. Also, the difference between
"GoTo 0" and "Resume Next" is that Resume Next is recommended for
whenever you are working with objects that might raise errors.

-- Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top