+ Reply to Thread
Results 1 to 10 of 10

Error Handling strange behaviour, it only works the first time

  1. #1
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Error Handling strange behaviour, it only works the first time



    Good day,

    I have used Error handling to prevent the macro from crashing and it works fine, except if you branch back to the body of the macro and hit the same error provoking condition again.!?!?

    The code below is used to search through a data base of chemicals and to extract the row of data for the chemical if found. The error handling is to prevent the macro from crashing if the chemical searched for is not present in the list.

    If the chemical searched for (search string) is not found, the operator is asked for a new search string, if the new search string is present the macro works fine, HOWEVER, if the new search string is also not present, the ERROR HANDLER is IGNORED!!!

    I would appreciate any help to sort out this, for me, STRANGE BEHAVIOUR.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Error Handling strange behaviour, it only works the first time

    Try this,

    Please Login or Register  to view this content.
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Error Handling strange behaviour, it only works the first time

    you can't use goto to exit an error handler-you have to use a resume statement

    anyway you don't really need an error handler there you can test directly if the string is found before trying to use the result
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Error Handling strange behaviour, it only works the first time

    also

    I'm afraid your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

  5. #5
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Error Handling strange behaviour, it only works the first time

    Hi Henk,

    Error Handler handles error one time only. You can try using Err.Number for multiple error handling.
    I have changed named ranges in your code for my easy sake. Below code should work with a bit tweaking from your end. Let me know if it works.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Error Handling strange behaviour, it only works the first time

    Quote Originally Posted by haripopuri View Post
    Hi Henk,

    Error Handler handles error one time only. You can try using Err.Number for multiple error handling.
    That is false. If that were true "On Error Resume Next" would only work once in a loop situation.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Error Handling strange behaviour, it only works the first time

    technically it's true. an error handler only handles the error that brought the processing to it. until you hit a resume statement (or leave the routine) any further error will be unhandled-which is the problem here. on error resume next effectively resets itself every time it is triggered so it only handles one error each pass

  8. #8
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Error Handling strange behaviour, it only works the first time

    Thank you guys, the problem has been solved with replacing "goto" with "Resume"

    http://www.vbaexpress.com/forum/newr...ote=1&p=280309

  9. #9
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Error Handling strange behaviour, it only works the first time

    Hi JapanDave,

    This didn't work, see explanation from JosephP, the goto is the problem.

    Thanks

  10. #10
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Error Handling strange behaviour, it only works the first time

    Quote Originally Posted by JosephP View Post
    also

    I'm afraid your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    Noted, thanks.

+ 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