+ Reply to Thread
Results 1 to 9 of 9

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

Hybrid View

Justin H2O Cells.Find in For Loop... 05-19-2022, 03:47 PM
carlmon Re: Cells.Find in For Loop... 05-19-2022, 04:00 PM
Justin H2O Re: Cells.Find in For Loop... 05-20-2022, 07:29 AM
carlmon Re: Cells.Find in For Loop... 05-19-2022, 04:08 PM
Justin H2O Re: Cells.Find in For Loop... 05-20-2022, 07:32 AM
hrlngrv Re: Cells.Find in For Loop... 05-19-2022, 04:59 PM
Justin H2O Re: Cells.Find in For Loop... 05-20-2022, 10:42 AM
hrlngrv Re: Cells.Find in For Loop... 05-23-2022, 12:24 AM
Justin H2O Re: Cells.Find in For Loop... 05-20-2022, 03:33 PM
  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.

    Sub FormatColumns()
    
    dim sht as Worksheet
    dim RollWorkbook as Workbook
    dim EncumbranceColumn as long
    dim Merchandise Column as long    
    
    For Each sht In RollWorkbook.Sheets
            On Error GoTo HANDLER_1:
                    EncumbranceColumn = Cells.Find(What:="*ENCUMBRANCE*", _
                                    After:=Cells(Rows.Count, Columns.Count), _
                                    LookAt:=xlPart, _
                                    LookIn:=xlFormulas, _
                                    SearchOrder:=xlByColumns, _
                                    SearchDirection:=xlPrevious, _
                                    MatchCase:=False).Column
                                    
                    If EncumbranceColumn > 0 Then
                        Columns(EncumbranceColumn).NumberFormat = "$#,##0.00"
                    End If
    HANDLER_1:
            Next sht
    
    
        For Each sht In RollWorkbook.Sheets
            On Error GoTo HANDLER_2:
                    MerchandiseColumn = Cells.Find(What:="*MERCHANDISE*", _
                                    After:=Cells(Rows.Count, Columns.Count), _
                                    LookAt:=xlPart, _
                                    LookIn:=xlFormulas, _
                                    SearchOrder:=xlByColumns, _
                                    SearchDirection:=xlPrevious, _
                                    MatchCase:=False).Column
                                    
                    If MerchandiseColumn > 0 Then
                        Columns(MerchandiseColumn).NumberFormat = "$#,##0.00"
                    End If
    HANDLER_2:
        Next sht
    
        RollWorkbook.Activate
      
    End Sub
    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!
    Sub FormatColumns()
    
    dim sht as Worksheet
    dim RollWorkbook as Workbook
    dim EncumbranceColumn as long
    dim Merchandise Column as long    
    
    For Each sht In RollWorkbook.Sheets
            On Error GoTo HANDLER_1:
                    EncumbranceColumn = Cells.Find(What:="*ENCUMBRANCE*", _
                                    After:=Cells(Rows.Count, Columns.Count), _
                                    LookAt:=xlPart, _
                                    LookIn:=xlFormulas, _
                                    SearchOrder:=xlByColumns, _
                                    SearchDirection:=xlPrevious, _
                                    MatchCase:=False).Column
                                    
                    If EncumbranceColumn > 0 Then
                        Columns(EncumbranceColumn).NumberFormat = "$#,##0.00"
                    End If
    HANDLER_1:
            Next sht
    
    
        For Each sht In RollWorkbook.Sheets
            On Error GoTo HANDLER_2:
                    MerchandiseColumn = Cells.Find(What:="*MERCHANDISE*", _
                                    After:=Cells(Rows.Count, Columns.Count), _
                                    LookAt:=xlPart, _
                                    LookIn:=xlFormulas, _
                                    SearchOrder:=xlByColumns, _
                                    SearchDirection:=xlPrevious, _
                                    MatchCase:=False).Column
                                    
                    If MerchandiseColumn > 0 Then
                        Columns(MerchandiseColumn).NumberFormat = "$#,##0.00"
                    End If
    HANDLER_2:
    Resume next
    Next sht RollWorkbook.Activate End Sub

  3. #3
    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!

  4. #4
    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

    dim Merchandise Column as long
    dim MerchandiseColumn as long
    but without the workbook it's difficult to tell.

  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

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

    dim Merchandise Column as long
    dim MerchandiseColumn as long
    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!

  6. #6
    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
    Sub FormatColumns()
      Dim sht As Worksheet
      Dim RollWorkbook As Workbook
      Dim EncumbranceCell As Range
      Dim MerchandiseCell As Range
        
      On Error Resume Next
    
      For Each sht In RollWorkbook.Worksheets
        Set EncumbranceCell = sht.Cells.Find( _
          What:="*ENCUMBRANCE*", _
          After:=Cells(Rows.Count, Columns.Count), _
          LookAt:=xlPart, _
          LookIn:=xlFormulas, _
          SearchOrder:=xlByColumns, _
          SearchDirection:=xlPrevious, _
          MatchCase:=False _
        )
                            
        If Not EncumbranceCell Is Nothing Then
          Intersect(sht.UsedRange, EncumbranceCell.EntireColumn).NumberFormat = "$#,##0.00"
        Else
          Err.Clear
        End If
    
        Set EncumbranceCell = Nothing
      Next sht
        
      For Each sht In RollWorkbook.Worksheets
        Set MerchandiseCell = sht.Cells.Find( _
          What:="*MERCHANDISE*", _
          After:=Cells(Rows.Count, Columns.Count), _
          LookAt:=xlPart, _
          LookIn:=xlFormulas, _
          SearchOrder:=xlByColumns, _
          SearchDirection:=xlPrevious, _
          MatchCase:=False _
        )
                            
        If Not MerchandiseCell Is Nothing Then
          Intersect(sht.UsedRange, MerchandiseCell.EntireColumn).NumberFormat = "$#,##0.00"
        Else
          Err.Clear
        End If
    
        Set MerchandiseCell = Nothing
      Next sht
    
      On Error GoTo 0
        
      RollWorkbook.Activate
          
    End Sub
    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

  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
    Sub FormatColumns()
      Dim sht As Worksheet
      Dim RollWorkbook As Workbook
      Dim EncumbranceCell As Range
      Dim MerchandiseCell As Range
        
      On Error Resume Next
    
      For Each sht In RollWorkbook.Worksheets
        Set EncumbranceCell = sht.Cells.Find( _
          What:="*ENCUMBRANCE*", _
          After:=Cells(Rows.Count, Columns.Count), _
          LookAt:=xlPart, _
          LookIn:=xlFormulas, _
          SearchOrder:=xlByColumns, _
          SearchDirection:=xlPrevious, _
          MatchCase:=False _
        )
                            
        If Not EncumbranceCell Is Nothing Then
          Intersect(sht.UsedRange, EncumbranceCell.EntireColumn).NumberFormat = "$#,##0.00"
        Else
          Err.Clear
        End If
    
        Set EncumbranceCell = Nothing
      Next sht
        
      For Each sht In RollWorkbook.Worksheets
        Set MerchandiseCell = sht.Cells.Find( _
          What:="*MERCHANDISE*", _
          After:=Cells(Rows.Count, Columns.Count), _
          LookAt:=xlPart, _
          LookIn:=xlFormulas, _
          SearchOrder:=xlByColumns, _
          SearchDirection:=xlPrevious, _
          MatchCase:=False _
        )
                            
        If Not MerchandiseCell Is Nothing Then
          Intersect(sht.UsedRange, MerchandiseCell.EntireColumn).NumberFormat = "$#,##0.00"
        Else
          Err.Clear
        End If
    
        Set MerchandiseCell = Nothing
      Next sht
    
      On Error GoTo 0
        
      RollWorkbook.Activate
          
    End Sub
    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
    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.

  9. #9
    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:

    Sub FormatTheF___ingColumns()
    
    Dim wrksht As Worksheet
    Dim ColumnToFormat As Long
    
    On Error Resume Next
    For Each wrksht In ThisWorkbook.Worksheets
    ColumnToFormat = 0
        ColumnToFormat = wrksht.Cells.Find( _
                         What:="*Encumbrance*", _
                         After:=Cells(wrksht.UsedRange.Row, wrksht.UsedRange.Column), _
                         LookIn:=xlValues, _
                         Lookat:=xlPart, _
                         Searchorder:=xlByColumns, _
                         Searchdirection:=xlNext, _
                         MatchCase:=False).Column
                         
        If ColumnToFormat <> 0 Then
            wrksht.Columns(ColumnToFormat).NumberFormat = "$#,##0.00"
        End If
    'Debug.Print ColumnToFormat
    Next wrksht
    
    Set wrksht = Empty
    
    On Error Resume Next
    For Each wrksht In ThisWorkbook.Worksheets
    ColumnToFormat = 0
        ColumnToFormat = wrksht.Cells.Find( _
                         What:="*Merchandise*", _
                         After:=Cells(wrksht.UsedRange.Row, wrksht.UsedRange.Column), _
                         LookIn:=xlValues, _
                         Lookat:=xlPart, _
                         Searchorder:=xlByColumns, _
                         Searchdirection:=xlNext, _
                         MatchCase:=False).Column
    
        If ColumnToFormat <> 0 Then
            wrksht.Columns(ColumnToFormat).NumberFormat = "$#,##0.00"
        End If
    Debug.Print ColumnToFormat
    Next wrksht
    
    End Sub

+ 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