+ Reply to Thread
Results 1 to 9 of 9

Cells.Find in For Loop Failing When Search Term Not Found

  1. #1
    Registered User
    Join Date
    12-30-2010
    Location
    New Haven, Ct
    MS-Off Ver
    Excel 2016
    Posts
    41

    Cells.Find in For Loop Failing When Search Term Not Found

    I'm hoping someone can help me out with this one.

    Please Login or Register  to view this content.
    This keeps failing on me with "RTE '91': Object variable or With block variable not set. It happens as soon as the second cells.find (MerchandiseColumn) doesn't find what it's looking for on the very first sheet. For the life of me I haven't been able to figure out what the deal is and why my On Error isn't working. Any help would be seriously appreciated.

    Justin

  2. #2
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Cells.Find in For Loop Failing When Search Term Not Found

    Try putting "Resume next" after your Errorhandler 2:

    Also, I'm from Branford, CT- howdy, neighbor!
    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Cells.Find in For Loop Failing When Search Term Not Found

    Also, your error may be coming from the way you declare

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    but without the workbook it's difficult to tell.

  4. #4
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Cells.Find in For Loop Failing When Search Term Not Found

    I'd be tempted to rewrite this as
    Please Login or Register  to view this content.
    I'm fastidious about error trapping. Also, this restricts number formatting to the used range in each worksheet. Maybe you want to extend this for many more rows, but I suspect you don't mean to extend it all the way down to row 1,048,576.

    Beyond that, if ENCUMBRANCE or MERCHANDISE are found in the (k-1)th worksheet but not the kth worksheet, the EncumbranceC.. and MerchandiseC.. variables would retain the values from the (k-1)th worksheet rather than be set to 0 or Nothing. You have to reset those variables just before Next in each For loop.

    Maybe not a potential problem, but avoid one anyway: if sht is type Worksheet, iterate over RollWorkbook.Worksheets rather than RollWorkbook.Sheets.

    Finally, use sht.Cells rather than just Cells. You're not activating different worksheets as you iterate through RollWorkbook.Worksheets, so unadorned Cells refers to ActiveSheet.Cells in each iteration.
    Last edited by hrlngrv; 05-19-2022 at 05:01 PM. Reason: typos

  5. #5
    Registered User
    Join Date
    12-30-2010
    Location
    New Haven, Ct
    MS-Off Ver
    Excel 2016
    Posts
    41

    Re: Cells.Find in For Loop Failing When Search Term Not Found

    I will definitely give this a go. Thank you!

  6. #6
    Registered User
    Join Date
    12-30-2010
    Location
    New Haven, Ct
    MS-Off Ver
    Excel 2016
    Posts
    41

    Re: Cells.Find in For Loop Failing When Search Term Not Found

    Quote Originally Posted by carlmon View Post
    Also, your error may be coming from the way you declare

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    but without the workbook it's difficult to tell.
    Good eye! This was my fault from when I transferred the block to my post. I checked my code and the space isn't in there. Thanks for looking out!

  7. #7
    Registered User
    Join Date
    12-30-2010
    Location
    New Haven, Ct
    MS-Off Ver
    Excel 2016
    Posts
    41

    Re: Cells.Find in For Loop Failing When Search Term Not Found

    Quote Originally Posted by hrlngrv View Post
    I'd be tempted to rewrite this as
    Please Login or Register  to view this content.
    I'm fastidious about error trapping. Also, this restricts number formatting to the used range in each worksheet. Maybe you want to extend this for many more rows, but I suspect you don't mean to extend it all the way down to row 1,048,576.

    Beyond that, if ENCUMBRANCE or MERCHANDISE are found in the (k-1)th worksheet but not the kth worksheet, the EncumbranceC.. and MerchandiseC.. variables would retain the values from the (k-1)th worksheet rather than be set to 0 or Nothing. You have to reset those variables just before Next in each For loop.

    Maybe not a potential problem, but avoid one anyway: if sht is type Worksheet, iterate over RollWorkbook.Worksheets rather than RollWorkbook.Sheets.

    Finally, use sht.Cells rather than just Cells. You're not activating different worksheets as you iterate through RollWorkbook.Worksheets, so unadorned Cells refers to ActiveSheet.Cells in each iteration.
    I tried using this because everything you're saying made so much sense but when I ran it, it didn't format Anything. I haven't had a chance to do any troubleshooting but when I do, I'll let you know what I find.

  8. #8
    Registered User
    Join Date
    12-30-2010
    Location
    New Haven, Ct
    MS-Off Ver
    Excel 2016
    Posts
    41

    Re: Cells.Find in For Loop Failing When Search Term Not Found

    Thanks everyone for your help! I decided to just start from scratch and it worked! Here is what I ended up going with:

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Cells.Find in For Loop Failing When Search Term Not Found

    Sorry. I should have spotted this before.

    I modified your original code, but I failed to spot that you hadn't initialized RollWorkbook. Without doing so when error trapping is active, the macro does nothing useful. If you initialize it using a Set statement, the rest of the macro works correctly.

+ 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. [SOLVED] searching a table for only parts of the search term, giving that part out if found
    By Oeko_Marvin in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 12-11-2020, 02:04 PM
  2. If search term is not found when naming a range.
    By Traziness in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-19-2020, 10:53 AM
  3. [SOLVED] Possible use of VBA to clear cell content if a search term is found
    By s_bruno1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-17-2017, 08:33 AM
  4. help with range.find method not finding search term
    By excel_joe in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-22-2016, 07:20 PM
  5. loop through workbooks to find term, find not working
    By bsapaka in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2015, 06:16 PM
  6. Replies: 2
    Last Post: 03-17-2014, 04:19 PM
  7. Replies: 3
    Last Post: 07-12-2006, 10:15 AM

Tags for this Thread

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