Searching for string within text

dugdugdug

Active Member
Joined
May 11, 2012
Messages
342
I am using the function Instr to search for a string within some text.

My list contains Apples and Applesauce.

How can I search for only Apples because I seem to be returning Applesuace as well.

Obviously the other way round is not a problem because searching for Applesauce will not return Apples.

Thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Something like:

Code:
InStr(" "&Range("A1")&" "," apples ")
i.e. add a space to search string, and space to A1 reference in cases Apples is at the beginning/end of the string.
 
Upvote 0
Try adding spaces to the beginning and end of the original string,
Then searching for the word also with spaces at the beginning and end..

Code:
Sub test()
mystr1 = "this is apples dude"
mystr2 = "and this is applesause my friend"
x = InStr(1, " " & mystr1 & " ", " apples ")
y = InStr(1, " " & mystr2 & " ", " apples ")
End Sub
 
Upvote 0
Something like:

Code:
InStr(" "&Range("A1")&" "," apples ")
i.e. add a space to search string, and space to A1 reference in cases Apples is at the beginning/end of the string.

Try adding spaces to the beginning and end of the original string,
Then searching for the word also with spaces at the beginning and end..

Code:
Sub test()
mystr1 = "this is apples dude"
mystr2 = "and this is applesause my friend"
x = InStr(1, " " & mystr1 & " ", " apples ")
y = InStr(1, " " & mystr2 & " ", " apples ")
End Sub

Sorry, I wasn't specific enough.

I have abcApplesedf
and efgApplesauceijk

so putting spaces would not help.
 
Upvote 0
You might have to use an AND to disqualify the "Applesauce"

e.g.


Code:
IF InStr(Range("A1"),"Apples")>0 And InStr(Range("A1"),"Applesauce")= 0 then
 x = InStr(Range("A1"),"Apples")
</pre>
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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