+ Reply to Thread
Results 1 to 8 of 8

best practice on error handling formulas in VBA

  1. #1
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    best practice on error handling formulas in VBA

    It is literally just a best practice question really.
    Is it better practice/more efficient when filling a formula (normally in my case a lookup) down a column in VBA to put any error handing in the formula something like
    Please Login or Register  to view this content.
    which is what I normally do, or is it better to write any error handling as part of the VBA routine i.e. an "On Error" piece of coding?

    Or is it personal preference either way?

    Any insight will be gratefully received (and yes in an ideal world I would love all the info at work not to trigger an error in the first place but sometimes it should)
    Last edited by WasWodge; 09-29-2011 at 05:36 PM.
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: best practice on error handling formulas in VBA

    Hi,

    If you're putting a formula into a range, and you want errors as a result of that formula to be translated into another type such as the string "Not Found", then I think it's very logical to handle the error in the formula as you have done. These are quite different to VBA run time errors, which is what you would use error handling for.
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: best practice on error handling formulas in VBA

    I'm not sure what the value is of evaluating the lookup twice just to change #N/A to "not found".
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: best practice on error handling formulas in VBA

    Sorry Shg, it wasn't a good example to use and I do get your point, but it wasn't / isn't part of any real code. I was just trying to establish the fundamentals of when to use the error handling in the formula and when in the coding itself.

    Thanks Colin for your pointers...much appreciated. I take it then that there is no obvious drawback to having it in the formula (that is at least when you are not doing unnecessary double evaluates)

  5. #5
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: best practice on error handling formulas in VBA

    I would say it has to do with your audience. As a developer, I would want to see the errors on the sheet as they are calculated. This way I can work on reducing or removing the source of the error, rather than just hiding it. If it is a report for users who have little to no Excel skills "Not Found" is so much more informative than "#N/A".

    If it is a question of performance, then go for the path of least amount of calculations. If it is for presentation, calculation time doesn't matter (as much). Of course, IFERROR() solves the issue of double evaluation, as long as all users have 2007 or higher.

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: best practice on error handling formulas in VBA

    Hi Paul,

    Sorry, but I don't fully understand the question. VBA error handling is quite different; what VBA error handling approach do you propose which gives an identical finish to the code you posted in #1? I'd be happy to compare it for you?

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: best practice on error handling formulas in VBA

    IMO, formulas (not VBA) return errors for one of four reasons:

    1. They are bad formulas and need to be corrected.

    2. They highlight deficiencies or missing data, and the result should be allowed to propagate to cue the user.

    3. They reflect unused cases or irrelevant conditions for the particular case at hand.

    4. They are used intentionally, e.g., to keep data from plotting.

    Only in case 3 (IMO) should you suppress the output using ISERROR or IFERROR.

  8. #8
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: best practice on error handling formulas in VBA

    Colin, in this case I didn't make myself clear in my last posting. I was referring in general that I wasn't doing anything wrong in using the error handling in formula when using VBA and not to the specific formula in post #1. I am happy now in my own mind that I am not doing anything stupid. Thanks for the guidance.

    Whizbang, makes sense. Cheers

    Shg, mainly everything I use the error handling in formula does fall into
    3. They reflect unused cases or irrelevant conditions for the particular case at hand.
    For instance I have a lot of dealings with things like shelf life but some lines do not have an official shelf life. Depending which category manager they come under then they are either marked as a blank, a 0 or some put in the text N/A. These are then downloaded to a text file and converted to excel and so from our point of view they fall under irrelevant conditions or unused cases when doing most monitoring work.

    Thanks to everyone for their input. Much clearer in my own mind now. Question solved
    Last edited by WasWodge; 09-29-2011 at 06:18 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1