Find/Findnext not returning results

bertjeiv

New Member
Joined
Sep 23, 2010
Messages
32
hello I try to find all occurrences of "spare" inside a certain range in all shapes and forms "xxspareyy" should also be returned, i believe lookat:=xlPart takes care of that? but i dont get any results. not even if the cell value is "spare"

here is the code lr1 is last row and lc1 is last column
Code:
    needle = "spare"
    spares = 0
    With Range(Cells(10, 1), Cells(lr1, lc1))
        Set lastcell = .Cells(.Cells.count)
    End With
    
    Set foundcell = Range(Cells(10, 1), Cells(lr1, lc1)).Find(What:=needle, after:=lastcell, LookAt:=xlPart, MatchCase:=False)
    If Not foundcell Is Nothing Then
        FirstAddr = foundcell.Address
        spares = spares + 1
    End If
    Do Until foundcell Is Nothing
        Debug.Print foundcell.Address
        Set foundcell = Range(Cells(10, 1), Cells(lr1, lc1)).FindNext(after:=foundcell)
        If foundcell.Address = FirstAddr Then
            Exit Do
        End If
        spares = spares + 1
    Loop
    MsgBox spares & " #spares"

I think I'm missing something stupid here but cant find it
Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
For certain arguments, Find uses the settings from the last Find (in the dialogue box or calling the method) if you omit the arguments - see the Help. Do a manual Find with the Macro Recorder and use the generated code in your code.
 
Upvote 0
Try:

Code:
    needle = "spare"
    spares = 0
    With Range(Cells(10, 1), Cells(lr1, lc1))
        Set lastcell = .Cells(.Cells.Count)
    End With
    With Range(Cells(10, 1), Cells(lr1, lc1))
        Set foundcell = .Find(What:=needle, LookIn:=xlValues, LookAt:=xlPart)
        If Not foundcell Is Nothing Then
            firstaddr = foundcell.Address
            spares = spares + 1
            Do
                Debug.Print foundcell.Address
                Set foundcell = .FindNext(after:=foundcell)
                spares = spares + 1
            Loop While Not foundcell Is Nothing And foundcell.Address <> firstaddr
        End If
    End With
    MsgBox spares & " #spares"
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top