Results 1 to 3 of 3

Thread: .FindNext error

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    .FindNext error

    I have this code where rngFound is a range. When I run it Excel highlights .FindNext and tells me "Method or data member not found". I've used .FindNext before without any problems. Why now?
    Code:
     
        With Sheet1       
            For lngRow = lngLastRowT To 2 Step -1
                Do
                    Set rngfound = wsWFD.Columns("E").Find(.Cells(lngRow, "E"), LookIn:=xlValues, LookAt:=xlWhole)
                    If Not rngfound Is Nothing Then
                        strFirstAddress = rngfound.Address
                        wsWFD.Cells(rngfound.Row, "A").EntireRow.Delete
                        Set rngfound = .FindNext(rngfound)
                        intCount = intCount + 1
                    End If
                Loop Until rngfound.Address = strFirstAddress
            Next
        End With

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,632

    Re: .FindNext error

    I would imagine if you check your other places you are using FindNext where it is working, you are using it against a Range. Here you are using it against a Worksheet.

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: .FindNext error

    I know I am late for the party but this may help any future visitor so I will go ahead and post a solution.

    The .Find is happening on wsWFD.Columns("E") so .FindNext should happen on that range.

    Code:
    Set rngfound = wsWFD.Columns("E").FindNext(rngfound)
    BTW, deleting in a loop can be extremely slow. You may want to identify the rows that you want to delete and store them using UNION. Finally at the end of the code delete it all together.

    Something like this (UNTESTED)

    Code:
        Dim DelRange As Range
        Dim rng As Range
        Dim bCell As Range
        
        Set rng = wsWFD.Columns("E")
        
        With Sheet1
            For lngRow = 2 To lngLastRowT
                Set rngfound = rng.Find(.Cells(lngRow, "E"), LookIn:=xlValues, LookAt:=xlWhole)
                
                If Not rngfound Is Nothing Then
                    Set bCell = rngfound
                                        
                    Set DelRange = wsWFD.Rows(rngfound.Row)
                       
                    Do
                        Set rngfound = rng.FindNext(rngfound)
                        
                        If Not rngfound Is Nothing Then
                            If rngfound.Address = bCell.Address Then Exit Do
                            
                            If DelRange Is Nothing Then
                                Set DelRange = wsWFD.Rows(rngfound.Row)
                            Else
                                Set DelRange = Union(DelRange, wsWFD.Rows(rngfound.Row))
                            End If
                        Else
                            Exit Do
                        End If
                    Loop
                End If
                
                If Not DelRange Is Nothing Then
                    DelRange.Delete
                    '~~> Set the range to nothing after deleting
                    Set DelRange = Nothing
                End If
                
                '~~> This is important to avoid false positives
                Set rngfound = Nothing
            Next
        End With
    PS: Long time! How have you been?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width