+ Reply to Thread
Results 1 to 8 of 8

Run-time error '91' when 'On error goto' and cells.find

  1. #1
    Registered User
    Join Date
    10-04-2008
    Location
    West Palm Beach
    Posts
    11

    Run-time error '91' when 'On error goto' and cells.find

    Hi all.

    I have written a Excel (2003) that searches a worksheet for a string in any cell. If the string is not found, it uses the 'On Error GoTo' command to jump to a given label. It works fine on the first string not found. When it searches for the next non-existent string, it fails with:

    'Run-time error '91':
    Object variable or With block variable not set'

    Do I have to clear a buffer after each cells.find search?

    My code:

    Please Login or Register  to view this content.

    Thanks in advance,


    Bjoern
    Last edited by bjoern; 10-09-2008 at 04:20 PM. Reason: Change status to solved.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    First, you may want to insert Exit Sub here
    Please Login or Register  to view this content.
    , unless you want to search for Car after finding Boat.

    You should also put
    Please Login or Register  to view this content.
    at the top of each of the error handling routines. Without being cleared, Excel thinks the error handling is still being done and any new errors will crash the routine.

    In general though, using error code like that is resulting in something approaching spaghetti code. A loop that avoids selecting and tests for whether the terms are found is possible.
    Please Login or Register  to view this content.
    That routine assumes that you want as many as possible of the search terms dealt with. If you want only the first found term handled, ingnoring the others, put
    Please Login or Register  to view this content.
    after the .Copy line.
    Last edited by mikerickson; 10-09-2008 at 12:19 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    10-04-2008
    Location
    West Palm Beach
    Posts
    11
    Quote Originally Posted by mikerickson View Post
    First, you may want to insert Exit Sub here
    Please Login or Register  to view this content.
    , unless you want to search for Car after finding Boat.

    You should also put
    Please Login or Register  to view this content.
    at the top of each of the error handling routines. Without being cleared, Excel thinks the error handling is still being done and any new errors will crash the routine.

    In general though, using error code like that is resulting in something approaching spaghetti code. A loop that avoids selecting and tests for whether the terms are found is possible.
    Please Login or Register  to view this content.
    That routine assumes that you want as many as possible of the search terms dealt with. If you want only the first found term handled, ingnoring the others, put
    Please Login or Register  to view this content.
    after the .Copy line.
    Thanks for the reply Mike. The routine works like a charm, however, instead of displaying error messages, I have to paste a '0' into the cell on the 'Main' sheet if the searchTerm on the 'Data" sheet isn't present.

    I will see if I can tweak this routine, in the meantime, I will close out this post.


    Thanks for your help,

    Bjoern
    Last edited by bjoern; 10-09-2008 at 03:54 PM.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Replace
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-04-2008
    Location
    West Palm Beach
    Posts
    11
    Quote Originally Posted by mikerickson View Post
    Replace
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    Thanks Mike,

    I replaced the msgbox with the code above, but I got a compile error when I started the macro.


    Bjoern

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Oops. Add one dot
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-04-2008
    Location
    West Palm Beach
    Posts
    11
    Quote Originally Posted by mikerickson View Post
    Oops. Add one dot
    Please Login or Register  to view this content.
    Hi Mike,
    then it fails with compile error at .Range.

    What I did is the following:
    Please Login or Register  to view this content.
    This works, but now I have to figure out how to past the '0' in the cell below the searchTerm instead of the next right cell.


    Thanks for you help thus far, Mike.


    Bjoern

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Just change the arguments of the .Offset

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. vba cells.find problem
    By jeternyn1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2007, 02:39 AM

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