error propagation in vba

  • Thread starter Thread starter steph
  • Start date Start date
S

steph

Hi group,

I've got this subs containing local error handling:


Private MySub()
On Error GoTo ErrTrap
Sub1
Sub2
...
Exit sub
ErrTrap:
If Err.Number = 18 Then
MsgBox "Error1"
Else
MsgBox "Error2"
End If
end


Sub1 and Sub2 do not contain any error handling.
My expection was that even if Error18 (User pressed <Esc>) happens in
Sub1 or Sub2 the error will propagate through to error handling of
MySub. But apparently that is not happening. Any ideas why this is so?
How can i change my code so that it's working as expected?

Thanks,
stephan
 
Hi group,

I've got this subs containing local error handling:

Private MySub()
   On Error GoTo ErrTrap
  Sub1
  Sub2
  ...
Exit sub
ErrTrap:
    If Err.Number = 18 Then
      MsgBox "Error1"
    Else
      MsgBox "Error2"
    End If
end

Sub1 and Sub2 do not contain any error handling.
My expection was that even if Error18 (User pressed <Esc>) happens in
Sub1 or Sub2 the error will propagate through to error handling of
MySub. But apparently that is not happening. Any ideas why this is so?
How can i change my code so that it's working as expected?

Thanks,
stephan

stephan,


What exactly is happening when the user presses ESC in sub 1 or 2?

Regards

David

David
 
stephan,

What exactly is happening when the user presses ESC in sub 1 or 2?

Regards

David

David

Hi,

A box is displayed saying it encountered error 18: break by user and
below there are some buttons "Resume" (disabled), "Stop", "Debug",
"Help". So it definitely does not jump into my dedicated error
handler.

regards,
stephan
 
Hi,

A box is displayed saying it encountered error 18: break by user and
below there are some buttons "Resume" (disabled), "Stop", "Debug",
"Help". So it definitely does not jump into my dedicated error
handler.

regards,
stephan- Hide quoted text -

- Show quoted text -

Stephan,

you need to place this line in your code

Application.EnableCancelKey = xlErrorHandler

you can place in anywhere before you call the subs.

That should work.

Regards

David
 
Stephan,

you need to place this line in your code

Application.EnableCancelKey = xlErrorHandler

you can place in anywhere before you call the subs.

That should work.

Regards

David

Hi,

Thanks for your help, but unfortunately it does not seem to work for
me: I've had this line already in my code but still I see the above
discribed behaviour.

regards,
stephan
 
Hi,

Thanks for your help, but unfortunately it does not seem to work for
me: I've had this line already in my code but still I see the above
discribed behaviour.

regards,
stephan

i checked this now: definitely, the error is not propagated to the
calling sub - the called subs need their own error handling, then it
works. i guess this is just how excel/vba behaves ...
 
Back
Top