View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
James Snell James Snell is offline
external usenet poster
 
Posts: 46
Default Error Handling construct

Thinking again...

Probably the best way of handling it is instead of using the additional on
error statements is to look for the specific error codes that might come from
the find.

myErrorHandler:

Select Case Err.Number

Case Is = 0
' Err code is 0 so no error - this in itself is an error
Case Is = -1 ' change this for the error code you get from an error in the
find.
Err.Clear
Resume Next
Case Else
''' do what you were going to do with your error handler anyway
End Select


That's probably a better solution than using multiple on error statements.

"Geoff" wrote:

Ok so it must be the On Error Resume Next before the Find statement in Main
that seems to disable myErrorHandler. So I'm thinking I should restate the
handler at that point instead of the following On Error GoTo 0?

Geoff

"James Snell" wrote:

On Error is procedural, so changing it in a child routine does not affect the
parent.

Proof of the pudding is in the eating though... So here's a sample.

Sub TestErr()
On Error GoTo errHandle
Call NaughtyChild
Err.Raise 2
MsgBox "error handler not called - oops"
End

errHandle:
MsgBox "error handler called - you do not need to restate the on error"
End Sub

Sub NaughtyChild()
On Error Resume Next
Err.Raise 1
Err.Clear
End Sub


"Geoff" wrote:

Hi
From tests and 'Help' it seems I need to restate On Error GoTo
myErrorHandler after returning from each sub proc and after the On Error
Resume Next.
Is that correct or is there a better way to provide error handling
throughout Main?

T.I.A.

Geoff

Sub Main()

On Error GoTo myErrorHandler

blah

SubProc1 ''no error handler in this proc

On Error Resume Next
foundIt = Sheets(1).Find("xxx", , , xlWhole)
On Error GoTo 0

SubProc2 ''no error handler in this proc

blah

Exit Sub

myErrorHandler:

If Err.Number < 0 Then
'''do something
Err.Clear
End If

End Sub