On Error Resume Next

M

Mike

I have seen "On Error Resume Next" in several different
macros but do not fully understand how it works. Any help
would be greatly appreciated. Thanks
 
D

Don Guillett

AFAIK it means what it says. If you encounter something that would cause an
error to stop your code, just ignore it and go on to the next thing which
may work. This may be good but it might ignore something you need to know
about.
 
M

Michael Hopwood

If a line of code is encountered that causes an error, "On Error Resume
Next" will not process that line of code but will resume execution on the
following line.

"On Error Resume Next" is quite handy if you want to handle errors "in-line"
rather than running a sub and ending the procedure:


On Error Resume Next
'Some line of code that returns an error here
If err.number = ErrorCodeToCheckFor then
'Do some stuff to handle the error
err.clear
 
O

Otto Moehrbach

Mike
Don and Michael gave you good info on what it does. I just want to add
one point.
Be aware that the instruction "On Error Resume Next" remains in force
until the end (End Sub or Exit Sub) of the current procedure. This is of no
consequence to you if the procedure consists of only the line that you
anticipated an error on. But if you have other things happening in the code
besides the one line that you thought might cause an error, you can have a
problem. If an error were to occur later but still in the same procedure,
Excel would simply "Resume Next". Then you would run around chasing your
tail trying to figure out why the data is not what you expected. Been
there.
You should always reinstate Excel's normal error procedures immediately
after the line that could cause the error. For example:
Sub MyMacro()
'Some code
On Error Resume Next
'The line that might cause an error
On Error GoTo 0
'More code
End Sub

The "On Error GoTo 0" returns error handling back to normal or default. HTH
Otto
 

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