Selecting FOUND result.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, how can i make vba to select 2nd FOUND result...
suppose from range.find("x"... i have 4 matching result found. How can i select the 2nd result. Is there a trick to do this?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Could you please make this work?
How do I make it stop when it the 2nd address/value of search result is selected.

And conditions to handle errors if there is no result and there is only one result.


Thanks again.

Code:
Sub FindSecondfromFound()
With Worksheets(1).Range("a1:a500")
    Set c = .Find(2, LookIn:=xlValues)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.Value = 5
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End With
End Sub
 
Upvote 0
If you just want the second result you don't need to loop. Just use .Find() to find the first one and then .FindNext() to find the second one.

Ex., finding the string "OK":

Code:
Sub FindSecondfromFound()
Dim c As Range
Dim firstAddress As String
 
With Worksheets(1).Range("a1:a500")
    Set c = .Find("OK", LookIn:=xlValues)
    If c Is Nothing Then
        MsgBox "Not found the first time"
    Else
        firstAddress = c.Address
        Set c = .FindNext(c)
        If c.Address = firstAddress Then
            MsgBox "Not found the second time"
        Else
            MsgBox "Address: " & c.Address
        End If
    End If
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

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