Capture results of Find function

asma410

New Member
Joined
Feb 1, 2005
Messages
35
Hello,
How do I capture the return value of a FIND function when the searched item cannot be found?

I am trying to locate 'mystring' in various worksheets but the variable 'strCheck' is always 'True' & vbNullString is always "", so how do I correctly capture the value returned from the search? The code below works fine but the results are not accurate. Please point out what I am doing wrong.
Thanks in advance.

With Range("A1:A2000")
On Error Resume Next

strCheck = Cells.Find(What:=mystring, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
On Error GoTo 0

End With

'if string is found....
If strCheck <> vbNullString Then
'do stuff
End if
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Use a range variable to store the return and check to see if it is nothing:

Code:
 Dim r As Range
 
Set r = Cells.Find(What:="My_sought_for_value")
 
If Not r Is Nothing Then
  'cell containg value found
Else
  'cell not found
End if
 
Upvote 0
Wow dint know it was so simple. I have been struggling all morning. Thank you so much Richard. Thats really helpful.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,809
Members
449,048
Latest member
greyangel23

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