I've never seen anyone teach people to useOn Error Goto -1
. Most people don't even know it exists in VBA. (and I'd just clarify it's VBA error handling, not Excel ) IMO, if you're using that, you've dug yourself into a hole with some bad code and gone looking round the internet for a solution that didn't involve rewriting your code properly.
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
On Error GoTo NoFoo1
Call CallByName(ws, "Foo1", VbMethod)
Call Foo2()
NoFoo1:
On Error GoTo -1
On Error GoTo 0
Next ws
Perhaps, but that adds wrapper functions and can create a bucket brigade of return code handling.There are simpler ways, such as moving your Callbyname code into a separate function that returns true if it succeeds.
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
On Error GoTo NoFoo1
Call CallByName(ws, "Foo1", VbMethod)
Call Foo2()
NoFoo1:
If Err.Number <> 0 Then
Call Foo3(Err.Number)
On Error GoTo -1 ' this clears Err.Number
End If
On Error GoTo 0
Next ws
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
try
{
Call CallByName(ws, "Foo1", VbMethod)
Call Foo2()
}
catch (ErrObject Err)
{
Call Foo3(Err.Number)
}
Next ws
It's a bit arcane apparently.Hmmm I am one of those who didn't know on error goto -1 existed...
Is this the same as on error goto 0? I don't even see -1 mentioned in the docs: On Error statement (VBA)