error propagation in vba

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
 
D

dheaton

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
 
S

steph

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
 
D

David Heaton

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
 
S

steph

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
 
S

steph

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 ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top