Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Error handling best practices


I'd like to get some ideas on error handling best practices. I prefer to
handle the errors where they occur using something like this

Set myWS = Nothing
on error resume next
Set myWS = ActiveWorkbook.Worksheets("Sheetabc")
On Error goto 0
if not myWS is nothing then
'Do Stuff
end if

I've also seen On Error GoTo ErrHandler with the error handler at the end.
I've been trying to update a workbook that has the latter and I'm finding
it's a pain to find the errors without making other code changes. Ideas
folks?

Thanks,
Barb Reinhardt
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Error handling best practices


A useful post
http://www.fmsinc.com/TPapers/vbacode/Debug.asp

If this post helps click Yes
---------------
Jacob Skaria


"Barb Reinhardt" wrote:

I'd like to get some ideas on error handling best practices. I prefer to
handle the errors where they occur using something like this

Set myWS = Nothing
on error resume next
Set myWS = ActiveWorkbook.Worksheets("Sheetabc")
On Error goto 0
if not myWS is nothing then
'Do Stuff
end if

I've also seen On Error GoTo ErrHandler with the error handler at the end.
I've been trying to update a workbook that has the latter and I'm finding
it's a pain to find the errors without making other code changes. Ideas
folks?

Thanks,
Barb Reinhardt

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 897
Default Error handling best practices

Keeping your error handling code separate from the logic of your
procedure is good programming practice.

You don't want "On Error" statements sprinkled throughout your code,
or multiple IF statements trying to test if errors occur. You don't
always know where errors can occur, so trying to hard code them leads
to hard-to-read code.

What I like to do is put "On Error GoTo ErrorHandler" at the top of a
procedure, then in the ErrorHandler section, you can test for what
type of error occurred.

i.e.
Select Case Err.Number
Case 7 ' Out of memory
Msgbox "Out of memory"
Case 11 ' Division by zero
Msgbox "Don't enter zero"
Case Else ' all other errors
Msgbox "An unspecified error occurred."
End Select

And so on. Of course, this approach requires that you are familiar
with what types of errors can occur, and what the error numbers are.

Having said that, I use your process sometimes. It depends on the
particulars of the procedure. In your particular case, you can
minimize the chance of an error occurring if you refer to the code
name of the sheet, not the sheet name. Sheet names are very easy to
change, but the code name stays the same.

HTH,
JP

On Jul 3, 6:46*am, Barb Reinhardt
wrote:
I'd like to get some ideas on error handling best practices. * *I prefer to
handle the errors where they occur using something like this

Set myWS = Nothing
on error resume next
Set myWS = ActiveWorkbook.Worksheets("Sheetabc")
On Error goto 0
if not myWS is nothing then
* * 'Do Stuff
end if

I've also seen On Error GoTo ErrHandler with the error handler at the end.. *
I've been trying to update a workbook that has the latter and I'm finding
it's a pain to find the errors without making other code changes. *Ideas
folks?

Thanks,
Barb Reinhardt


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Error handling best practices


i addition, where you can, make assignments in functions that handle the
errors outside of your main code
, which IMHO gives better error control.
Every developer has their pet loves & hates, this is one of mu favourites.

so instead of this

Set myWS = ActiveWorkbook.Worksheets("Sheetabc")

I'd have

Set myWS = GetSheet("Sheetabc")
if myWS Is Nothing then
'handle no sheet problem here
end if


Function GetSheet(sheetname as string, optional bAdd as Boolean=False) As
Worksheet
on error resume next
SET GetSheet.Worksheets(sheetname )
if err.Number<0 then 'sheet doesn't exist
err.clear
if bAdd then
SET GetSheet.Worksheets.Add
GetSheet.Name = "Sheetabc"
end if
End If
On Error goto 0
End Function


This means that the ANTICIPATED error is handled correctly by your code and
the main routine's error handler can be left to look after unanticipated
issues.

"Barb Reinhardt" wrote in message
...
I'd like to get some ideas on error handling best practices. I prefer
to
handle the errors where they occur using something like this

Set myWS = Nothing
on error resume next
Set myWS = ActiveWorkbook.Worksheets("Sheetabc")
On Error goto 0
if not myWS is nothing then
'Do Stuff
end if

I've also seen On Error GoTo ErrHandler with the error handler at the end.
I've been trying to update a workbook that has the latter and I'm finding
it's a pain to find the errors without making other code changes. Ideas
folks?

Thanks,
Barb Reinhardt


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
First Error works but Subsequent Error Handling Does Not Edwin Kelly Excel Programming 2 May 20th 09 02:40 PM
Error handling error # 1004 Run-time error [email protected] Excel Programming 3 May 20th 08 02:23 PM
Set Error handling INSIDE error-trap Michelle Excel Programming 6 May 3rd 08 03:30 PM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Error handling with a handling routine ben Excel Programming 0 March 15th 05 03:01 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"