ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error Handling - Did i do it correct (https://www.excelbanter.com/excel-programming/367053-error-handling-did-i-do-correct.html)

Desert Piranha[_88_]

Error Handling - Did i do it correct
 

Hi All,

I know error handling can be tricky.
So i have this in some code i am working on. i just want to make sure i
am doing it correctly.

I have this at the very top:

Code:
--------------------
On Error GoTo ErrHandler:
--------------------


This is at the very bottom"

Code:
--------------------
Exit Sub
ErrHandler: ActiveSheet.Protect
MsgBox ("You did not open ''UCPSITE-06.xls''")
--------------------

This code is copying data from another open workbook, then pasting into
this one.
If the other workbook is not open, i want to exit sub and show message
box.

It seems to be working, but i wanted to get a guru's opinion.


--
Desert Piranha


------------------------------------------------------------------------
Desert Piranha's Profile: http://www.excelforum.com/member.php...o&userid=28934
View this thread: http://www.excelforum.com/showthread...hreadid=560960


witek

Error Handling - Did i do it correct
 
Desert Piranha wrote:
Hi All,

I know error handling can be tricky.
So i have this in some code i am working on. i just want to make sure i
am doing it correctly.

I have this at the very top:

Code:
--------------------
On Error GoTo ErrHandler:
--------------------


This is at the very bottom"

Code:
--------------------
Exit Sub
ErrHandler: ActiveSheet.Protect
MsgBox ("You did not open ''UCPSITE-06.xls''")
--------------------

This code is copying data from another open workbook, then pasting into
this one.
If the other workbook is not open, i want to exit sub and show message
box.

It seems to be working, but i wanted to get a guru's opinion.




OK

NickHK

Error Handling - Did i do it correct
 
The line:
On Error GoTo ErrHandler:
creates an Active error handler. Errors "bubble up" through the stack until
such an active error handler is found. If none is found, that when you get
the VBA msgbox saying "Error: Subscript out range" or whatever and your code
abruptly stops.
See if the example below helps see the possiblities:

Private Sub CommandButton1_Click()
Call StartMacro
End Sub

Sub StartMacro()
On Error GoTo Handler:
Call Sub1
Exit Sub
Handler:
'Select case Err.Number...etc
MsgBox "Error handled in StartMacro" & vbNewLine & Err.Description
End Sub

Sub Sub1()
On Error GoTo Handler
Call Func1
Worksheets(0).Select 'Create an Error
Exit Sub
Handler:
Select Case Err.Number
Case 11 'Division by 0
MsgBox "Error handled in Sub1"
Resume Next
Case Else
Dim errnum As Long
MsgBox "Passing error up the stack"
errnum = Err.Number
Err.Raise errnum, "Sub1", "Not 1/0 error"
End Select
End Sub

Function Func1() As Long
'No error handling in this function
Func1 = 1 / 0 'Create an Error
End Function

NickHK

"Desert Piranha"
<Desert.Piranha.2av868_1152763211.4921@excelforu m-nospam.com wrote in
message news:Desert.Piranha.2av868_1152763211.4921@excelfo rum-nospam.com...

Hi All,

I know error handling can be tricky.
So i have this in some code i am working on. i just want to make sure i
am doing it correctly.

I have this at the very top:

Code:
--------------------
On Error GoTo ErrHandler:
--------------------


This is at the very bottom"

Code:
--------------------
Exit Sub
ErrHandler: ActiveSheet.Protect
MsgBox ("You did not open ''UCPSITE-06.xls''")
--------------------

This code is copying data from another open workbook, then pasting into
this one.
If the other workbook is not open, i want to exit sub and show message
box.

It seems to be working, but i wanted to get a guru's opinion.


--
Desert Piranha


------------------------------------------------------------------------
Desert Piranha's Profile:

http://www.excelforum.com/member.php...o&userid=28934
View this thread: http://www.excelforum.com/showthread...hreadid=560960




Desert Piranha[_89_]

Error Handling - Did i do it correct
 

NickHK Wrote:
The line:
On Error GoTo ErrHandler:
creates an Active error handler. Errors "bubble up" through the stac
until
such an active error handler is found. If none is found, that when yo
get
the VBA msgbox saying "Error: Subscript out range" or whatever and you
code
abruptly stops.
See if the example below helps see the possiblities:

Private Sub CommandButton1_Click()
Call StartMacro
End Sub

Sub StartMacro()
On Error GoTo Handler:
Call Sub1
Exit Sub
Handler:
'Select case Err.Number...etc
MsgBox "Error handled in StartMacro" & vbNewLine & Err.Description
End Sub

Sub Sub1()
On Error GoTo Handler
Call Func1
Worksheets(0).Select 'Create an Error
Exit Sub
Handler:
Select Case Err.Number
Case 11 'Division by 0
MsgBox "Error handled in Sub1"
Resume Next
Case Else
Dim errnum As Long
MsgBox "Passing error up the stack"
errnum = Err.Number
Err.Raise errnum, "Sub1", "Not 1/0 error"
End Select
End Sub

Function Func1() As Long
'No error handling in this function
Func1 = 1 / 0 'Create an Error
End Function

NickHKHi NickHK,


WOW

Thank you so much for this. Very cool

--
Desert Piranh

-----------------------------------------------------------------------
Desert Piranha's Profile: http://www.excelforum.com/member.php...fo&userid=2893
View this thread: http://www.excelforum.com/showthread.php?threadid=56096



All times are GMT +1. The time now is 05:47 PM.

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