![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
|
I have a fairly lengthy routine which seems to be fairly tight, but unfortunately will be used by our QC dept, who can break almost anything
Public Sub Main() Call Sub1 Call Sub2 Call Sub3 call Sub4 End Sub If I wrap an error trap around the above, will any errors thrown up in the called subs be caught by it? Or do I need a separate trap within each sub? Thanks for any help. |
|
|
|
|
|
#2 |
|
Guest
Posts: n/a
|
[quote]
I have a fairly lengthy routine which seems to be fairly tight, but unfortunately will be used by our QC dept, who can break almost anything . The main part of the routine looks similar to this: - Public Sub Main() Call Sub1 Call Sub2 Call Sub3 call Sub4 End Sub If I wrap an error trap around the above, will any errors thrown up in the called subs be caught by it? Or do I need a separate trap within each sub? Thanks for any help. [quote] Hi Mudface, The answer to your question is yes. If Sub Main has an error handling statement and an error occurs in Sub1 the error will be handled in Sub Main. However, if you have a statement in Sub1 which handles errors then that will take priority over the one in Sub Main. Another point worth bearing in mind is that if in Sub1 you have a line like On Error Goto ErrSub1 but then later you use On Error Goto 0 error handling will be return to Sub Main or whatever sub called Sub1. If you set a breakpoint on Proc2 and step through this code you'll see what I mean. HTH, Dan. Code:
Sub Main() On Error GoTo errHandler Proc2 Exit Sub errHandler: MsgBox "Error " & Err.Description End Sub Sub Proc2() On Error GoTo errhndle Dim x As Long x = 1 / 0 On Error GoTo 0 x = 1 / 0 errhndle: MsgBox "Error in proc2" Resume Next End Sub |
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
|
BTW, that was me - it's too early on a Sunday to be doing this
Regards, Dan |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
|
Thanks Dan, that'll save a fair bit of typing, I just wanted something simple to show there had been an error and that I should be notified, rather than having the user dumped out unceremoniously.
I was going to try and raise errors within each of the subs and see what happened, but forgot to bring the file home with me Early?! Not when you've been up since 4 with a 4 month old with a cold... [ This Message was edited by: Mudface on 2002-03-10 05:51 ] |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|