+ Reply to Thread
Results 1 to 13 of 13

Error Handling behaving weirdly

  1. #1
    Forum Contributor
    Join Date
    09-23-2008
    Location
    UK
    Posts
    137

    Error Handling behaving weirdly

    Any ideas why, rather than returning to the output line, this just goes to the next error handler?

    Say for example cells(count,1) produces an error, then it goes to the GeneralErrHandler. But when it gets to the resume statement, it simply goes to the DateErrHandler and then to outputline rather than straight to outputline??


    Please Login or Register  to view this content.
    Last edited by firefly2k8; 09-10-2010 at 09:47 AM.

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Error Handling behaving weirdly

    A couple of observations.
    1) this code does compile as the line "input = " uses the reserved word "input" as a variable name.
    2) There are simpler ways to do this type of error handling. For example
    Please Login or Register  to view this content.
    Date errors would be handled similarly. Check input the Err object for what other kind of information it can provide you about an error.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Forum Contributor
    Join Date
    09-23-2008
    Location
    UK
    Posts
    137

    Re: Error Handling behaving weirdly

    In the actual code i don't use the parameter input - but good spot.

    I like your idea here. But the error typicaly occurs when the following function fails to find a lookup value:

    On Error Resume Next
    SearchCol = Application.Match(Pair, Range(BBDataSearchRow & ":" & BBDataSearchRow), 0)

    Using a watch, it seems err.number remains zero even when "on error resume next" prevents execution failure.

    Is there any way I can detect when SearchCol returns an error without using On Error Goto... ?
    Last edited by firefly2k8; 09-10-2010 at 09:08 AM. Reason: poor typing skills

  4. #4
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Error Handling behaving weirdly

    Not familar with Application.Match. It's not in Excel 2003, but you can use IsEmpty or worksheet functions like IsNa to check for values returned that are not considered VBA errors.

  5. #5
    Forum Contributor
    Join Date
    09-23-2008
    Location
    UK
    Posts
    137

    Re: Error Handling behaving weirdly

    I hope it is excel 2003, since I am using excel 2003!!

    i'll try your suggestion.

    Thank you

  6. #6
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Error Handling behaving weirdly

    Oh, I normally use the Application.WorksheetFunction qualifier. I didn't know you were referring to the Excel Match function. Hope my suggestion helps.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,264

    Re: Error Handling behaving weirdly

    Application.Match does not cause a run-time error (unlike WorksheetFunction.Match) which is why the err.number is 0. You need to store the result in a Variant and check it using Iserror:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  8. #8
    Forum Contributor
    Join Date
    09-23-2008
    Location
    UK
    Posts
    137

    Re: Error Handling behaving weirdly

    I was advised the application.worksheetfunction.match was unreliable and i should use application.match instead.

    however, sometimes when application.match fails to find a value it returns Error 2402 - in which case iserror picks it up nicely - happy days.

    but sometimes when application.match fails to find a value it returns 0!

    i can do: if iserror(SearchCol) or SearchCol = 0

    but it is starting to get clumsy!

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,264

    Re: Error Handling behaving weirdly

    Quote Originally Posted by firefly2k8 View Post
    I was advised the application.worksheetfunction.match was unreliable and i should use application.match instead.
    By whom?

    but sometimes when application.match fails to find a value it returns 0!
    I have never seen that.
    The only way I can see that happening would be if you declared SearchCol as an integer or long and put an on error resume next statement in front of it. Then SearchCol would be 0, but that would not be what Application.Match actually returned - it always returns Error 2042 if the value is not found.

  10. #10
    Forum Contributor
    Join Date
    09-23-2008
    Location
    UK
    Posts
    137

    Re: Error Handling behaving weirdly

    you are quite correct romperstomper, I had SearchCol defined as long. With it defined as a variant all is working.

    I was advised about application.match on this forum when i encountered a different problem. can't recall who/what/where/why.

    Thanks for your help all.

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,264

    Re: Error Handling behaving weirdly

    For the record, the worksheetfunction version is not unreliable - the only difference is that it raises a run-time error if the value is not found.

    Please don't forget to mark the thread 'Solved'.

  12. #12
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Error Handling behaving weirdly

    Quote Originally Posted by romperstomper View Post
    For the record, the worksheetfunction version is not unreliable - the only difference is that it raises a run-time error if the value is not found.

    Please don't forget to mark the thread 'Solved'.
    So would another way to approach this would be to use the worksheetfunction version and an On Error? Does this approach work from all worksheetfunctions?

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,264

    Re: Error Handling behaving weirdly

    Yes, and yes. (AFAIK, anything you can do via the WorksheetFunction object you can do via Application instead)

+ 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