Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Sam Sam is offline
external usenet poster
 
Posts: 699
Default HELP: error handling in VBA

Hi All,

Why do we use "On error resume next" shouldnt we be resolving those errors.

For eg: if we are sending important email notifications through excel VBA
which affect management decisions, shouldnt we be avoiding to user "On error
resume next"? IF for some reason there is an error and an email is not sent
it might affect a lot of things.

My concern is: how do we handle error is this situations? is there a better
way to handle errors, rather than using "On error resume next" statement?

Thanks in advance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default HELP: error handling in VBA

Hi
Use "on error resume next" when you are not bothered what the error is
or want to use the error. Call a sub if you want to handle the error
or use goto.
As an example of using "on error resume next", if you want to add a
comment to a cell and put in some text you may not be bothered if the
comment and text exists already. In that case, the error is thrown
(but ignored) if the comment already exists but is not thrown if there
is no comment there, and you can add it. Your code is simpler - you
don't need to check the comment is there. This principle can be used
for lots of things - do variables exist, do range names exist yet, do
sheets exist yet etc.
regards
paul

On Mar 8, 6:47*pm, sam wrote:
Hi All,

Why do we use "On error resume next" shouldnt we be resolving those errors.

For eg: if we are sending important email notifications through excel VBA
which affect management decisions, shouldnt we be avoiding to user "On error
resume next"? IF for some reason there is an error and an email is not sent
it might affect a lot of things.

My concern is: how do we handle error is this situations? is there a better
way to handle errors, rather than using "On error resume next" statement?

Thanks in advance.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 834
Default error handling in VBA

On Error Resume Next can be very useful if we want test a certain condition
that may or may not be met, and then test the results afterwards. That is
testing for predictable errors that we don't want to crash and burn on.

For instance, suppose we intend to add a worksheet, but it may already have
been added. We can use code like this

On Error Resume Next
Set ws = Worksheets("mySheet")
On Error Goto 0
If ws Is Nothing Then

Set ws = Worksheets.Add
ws.Name = "mySheet"
End If

'then do stuff with ws

So as you can see, it is a useful technique to help us test things. The
important thing is to make sure that the scope of an On Error Resume Next is
very limited, as I did above by restricting it to just one line of code, so
that real errors don't just get ignored.

The better way is proper error handling, like this

On Error GoTo errHandler

'do stuff like sending mail

Exit Sub 'so we don't drop into the error handler

errHandler:
MsgBox "Unexpected error" & vbNewline & _
"Error: " & Err.Number & ", " & Err.Description, _
vbOkOnly & vbCritical, "My App - Error"

--

HTH

Bob

"sam" wrote in message
...
Hi All,

Why do we use "On error resume next" shouldnt we be resolving those
errors.

For eg: if we are sending important email notifications through excel VBA
which affect management decisions, shouldnt we be avoiding to user "On
error
resume next"? IF for some reason there is an error and an email is not
sent
it might affect a lot of things.

My concern is: how do we handle error is this situations? is there a
better
way to handle errors, rather than using "On error resume next" statement?

Thanks in advance.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default HELP: error handling in VBA

Here's how I trap errors.

Sub ErrorTesing()

On Error GoTo ErrStop

' enter vba code here like - email code

'Note: you do not have to put Exit Sub
'the error free code passes through the ErrStop:
'& ends on End Sub

ErrStop:
If Err Then MsgBox ("Error Occurred " & Err)
End Sub

If an error is detected in your code it will be displayed in a MsgBox
"Error Occurred xxx"

where xxx is the error number

HTH,
--
Data Hog


"sam" wrote:

Hi All,

Why do we use "On error resume next" shouldnt we be resolving those errors.

For eg: if we are sending important email notifications through excel VBA
which affect management decisions, shouldnt we be avoiding to user "On error
resume next"? IF for some reason there is an error and an email is not sent
it might affect a lot of things.

My concern is: how do we handle error is this situations? is there a better
way to handle errors, rather than using "On error resume next" statement?

Thanks in advance.

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
Err.Description as error proc name trail, error handling Neal Zimm Excel Programming 4 February 25th 10 08:07 AM
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 02:17 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"