Before I go into the details, your main routine should have an error handler like this one:
Sub Main()
On Error GoTo Errorhandler
'Your code
Exit Sub
Errorhandler:
If Err.Source = "" Then Err.Source = Application.Name
Debug.Print "Source : " & Err.Source
Debug.Print "Error : " & Err.Number
Debug.Print "Description: " & Err.Description
If MsgBox("Error " & Err.Number & ": " & vbNewLine & vbNewLine & _
Err.Description & vbNewLine & vbNewLine & _
"Enter debug mode?", vbOKCancel + vbDefaultButton2, Err.Source) = vbOK Then
Stop 'Press F8 twice
Resume
End If
End Sub
And all your subroutines can also install error handlers.
And in terms of the code above, they should write the name of the subroutine into Err.Source, e.g.:
Err.Source = "DoSomething"
At any point inside a sub routine, when an unhandled error occurs (e.g. after
On Error Goto 0), the code jumps back into the error handler inside the main routine.
Okay, back to your question, basically you think you could do that:
Sub Wrong()
On Error GoTo Errorhandler
Err.Raise 1
Exit Sub
Errorhandler:
On Error Resume Next
Err.Raise 2
End Sub
This does not work as expected because you do not call Resume! And that is the secret:
Sub Right()
On Error GoTo Errorhandler
Err.Raise 1, "Sub Right", "Error description 1"
Exit Sub
Errorhandler:
Select Case Err.Number
Case 1
Resume Errorhandler
Case 2
'Error is not trappable
Exit Sub
End Select
Err.Raise 2, "Sub Right", "Error description 2"
End Sub
Copy sub Right into the VBA editor and debug it (execute it step by step by pressing F8 key). Then you should understand whats going on. Afterwards debug also the subs below my name, which are other possibilities.
Andreas.
Sub Right2()
On Error GoTo Errorhandler
Err.Raise 1, "Sub Right", "Error description 1"
Exit Sub
Errorhandler:
Select Case Err.Number
Case 1
Resume Errorhandler
Case 2
Resume Next
End Select
Err.Raise 2, "Sub Right", "Error description 2"
'Just a line of code:
DoEvents
End Sub
Sub Right3()
On Error GoTo Errorhandler
Err.Raise 1, "Sub Right", "Error description 1"
Exit Sub
Errorhandler:
Select Case Err.Number
Case 1
Resume Errorhandler
Case 2
On Error Resume Next
Resume Errorhandler
End Select
Err.Raise 2, "Sub Right", "Error description 2"
'Just a line of code:
DoEvents
End Sub