-
.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
-
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.
-
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?