View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 1,726
Default On Error Goto 0

It is most typically used in a block of code that you know may error, but
don't want a code failure if it does. To stop this, you put code to ignore
the error

On Error Resume Next

then the code that might fail, and then to get back to the status quo,
disable the error handling with

On Error Goto 0

As an example, if you want to test whether a worksheet exists, you could set
a worksheet variable to that worksheet. If it doesn't exist, this error
handling will stop a failure, and it can then be tested

On Error Resume Next
Set sh = Worksheets("somename")
On Error Goto 0
If sh Is Nothing Then
Msgbox "Sheet does not exist"
End If


Here is a more interesting example. At first sight you might think it would
loop in the error handler in the child routine, or after the first error we
would get no more error handling. In fact, it is neither. Try it and see

Sub DemoErrors()

On Error GoTo err_handler_1

Call DemoChild

err_handler_1:
MsgBox "Error handled in parent routine"
End Sub

Sub DemoChild()
Dim some_var

On Error GoTo err_handler_2

'force an error
some_var = 1 / 0
MsgBox "this shouldn't happen"
Exit Sub

err_handler_2:
MsgBox "We got an error in child routine"
'disable the current error handler
On Error GoTo 0

'now force another error
some_var = 1 / 0
End Sub



--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


"Perico" wrote in message
...
What does this do that having no Error catcher would not do? How is this
used? In testing, it simply takes you to the error; but wouldn't having
no
"On Error" expresion do the same thing? Or, is this used when you want to
trap an error in a specific part of your code?