ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   On Error GoTo If (https://www.excelbanter.com/excel-programming/332561-error-goto-if.html)

Jean-Jérôme Doucet via OfficeKB.com

On Error GoTo If
 
Hi,
I posted a post earlier about error management in VBA, but I need a bit more
info on that.

I need My On Error GoTo to point to an If condition that will be activated
only by this error and and want to stop the complete macro, not only the sub.
Actually, I'm using multiple sub.

Thanks!

JJD

--
Message posted via http://www.officekb.com

Bob Phillips[_6_]

On Error GoTo If
 
Exit Sub before the error handle to ensure the other code doesn't drop into
it, and use End to stop the whole app.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jean-Jerome Doucet via OfficeKB.com" wrote in
message ...
Hi,
I posted a post earlier about error management in VBA, but I need a bit

more
info on that.

I need My On Error GoTo to point to an If condition that will be activated
only by this error and and want to stop the complete macro, not only the

sub.
Actually, I'm using multiple sub.

Thanks!

JJD

--
Message posted via http://www.officekb.com




bhofsetz[_70_]

On Error GoTo If
 

JJD,
Have your On Error GoTo statement point to a specific line in th
sub where you are trying to trap the error, such as:

On Error GoTo errhndlr

Then at the bottom of your sub put the following

Exit Sub
errhndlr:
' - Then put whatever code you want to run if the error occurs
' - The IF statement in your case
' - IF your condition is true then use END to quit the entire macro

Be sure to have Exit Sub before your error handler so it will not ru
unless an error occurs.
Also make sure your errhndlr: line starts all the way to the left (i
not indented) and that it ends with the colon :

HT

--
bhofset
-----------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...fo&userid=1880
View this thread: http://www.excelforum.com/showthread.php?threadid=38134


Tom Ogilvy

On Error GoTo If
 
Put your error handler only in the topmost SUB. Then if there is an error
in a sub called by this sub, it will use the error handler in the topmost
sub


Sub Main()
On Error GoTo ErrHandler
Macro1
Macro2
Macro3

Exit Sub
ErrHandler:
MsgBox "In Error Handler"
End Sub

Sub Macro1()
MsgBox "In Macro1"
End Sub

Sub Macro2()
MsgBox "In Macro2 before Macro4"
Macro4
MsgBox "In Macro2 after Macro4"
End Sub

Sub Macro4()
MsgBox "In Macro4 Before Error"
Err.Raise 5000
MsgBox "In Macro4 After Error"
End Sub

Sub Macro3()
MsgBox "In Macro3"
End Sub


--
Regards,
Tom Ogilvy




"Jean-Jerome Doucet via OfficeKB.com" wrote in
message ...
Hi,
I posted a post earlier about error management in VBA, but I need a bit

more
info on that.

I need My On Error GoTo to point to an If condition that will be activated
only by this error and and want to stop the complete macro, not only the

sub.
Actually, I'm using multiple sub.

Thanks!

JJD

--
Message posted via http://www.officekb.com




Jean-Jerome Doucet via OfficeKB.com

On Error GoTo If
 
Ok, I understood that if you put your label at the end of the sub with an
exit sub just before this label, it will only be executed if the error goto
intruction say to go there. The only problem I seem to have is that if I
click cancel or write text in my inputbox, it seems to continue without using
my error handler. Strange.

Werner

Quote:
On Error GoTo IndexError
[...] 'code I just skipped in this quote

Exit Sub 'if no errors exit procedure

IndexError:

MsgBox "Dead end!"
End
Resume

End Sub

--
Message posted via http://www.officekb.com

Bob Phillips[_6_]

On Error GoTo If
 
Cancel in an inputbox is not an error. You have to trap that condition and
act accordingly.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jean-Jerome Doucet via OfficeKB.com" wrote in message
...
Ok, I understood that if you put your label at the end of the sub with an
exit sub just before this label, it will only be executed if the error

goto
intruction say to go there. The only problem I seem to have is that if I
click cancel or write text in my inputbox, it seems to continue without

using
my error handler. Strange.

Werner

Quote:
On Error GoTo IndexError
[...] 'code I just skipped in this quote

Exit Sub 'if no errors exit procedure

IndexError:

MsgBox "Dead end!"
End
Resume

End Sub

--
Message posted via http://www.officekb.com




bhofsetz[_72_]

On Error GoTo If
 

What error are you trying to trap?

Put the input box code and what error this is raising in your post

--
bhofset
-----------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...fo&userid=1880
View this thread: http://www.excelforum.com/showthread.php?threadid=38134


Jean-Jerome Doucet via OfficeKB.com

On Error GoTo If
 
Thanks for your help Tom Ogilvy! It helped me on my issue.

Regards,

Werner

--
Message posted via http://www.officekb.com


All times are GMT +1. The time now is 04:44 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com