IF the first word of one cell is found within the contents of another...return value A

jeffcoleky

Active Member
Joined
May 24, 2011
Messages
274
Code:
=IF(A2="","EMPTY",(IF(ISNUMBER(SEARCH(A2,B2)),"Match","No-Match")))

I currently use the above formula to find out if the contents of cell A2 are included in the cell B2. It works well when there is only 1 word in A2. When there is more than one, it tries to match the entire cell and I need it to only match the 1st.

How do I change this to search for only the FIRST word of A2 in cell B2, and return "Match" if it is found, and "No-Match" if not?

Your help is much appreciated.
 
Not to put another question within this post, but would be easy to change this formula to search cell B2 for ANY word in cell A2, rather than just the first word of A2?


Two parts

VBA Macro
Code:
Public Function Findany(sLookupList As Range, sSearchStr As Range) As Long
Application.Volatile
Dim sLocalLookup As String
Dim sLocalSearchStr As String
Dim sTest As String
On Error Resume Next
Dim lMatches As Long
Findany = 0
lMatches = 0
sLocalLookup = Trim(UCase$(sLookupList.Value))
sLocalSearchStr = Trim(UCase$(sSearchStr.Value))

If sLocalLookup = "" Then
   Findany = -1
   Exit Function
End If
While Len(Trim(sLocalLookup)) > 0
   'extract search item from the left of the cell/string
   sTest = Trim(IIf(InStr(1, sLocalLookup, " ") > 0, Left(sLocalLookup, InStr(1, sLocalLookup, " ")), Trim(sLocalLookup)))
   'test for presence in search string
   If InStr(1, sLocalSearchStr, sTest) > 0 Then
      lMatches = lMatches + 1 ' if present
   End If
   'remove search items just checked
   sLocalLookup = Trim(Replace(sLocalLookup, sTest, ""))
   
   DoEvents
Wend
Findany = lMatches
Exit Function
End Function

and Function

=IF(Findany(A16,C16)=-1,"Empty",IF(Findany(A16,C16)>0,"Match","No-Match"))
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Two parts

VBA Macro
Code:
Public Function Findany(sLookupList As Range, sSearchStr As Range) As Long
Application.Volatile
Dim sLocalLookup As String
Dim sLocalSearchStr As String
Dim sTest As String
On Error Resume Next
Dim lMatches As Long
Findany = 0
lMatches = 0
sLocalLookup = Trim(UCase$(sLookupList.Value))
sLocalSearchStr = Trim(UCase$(sSearchStr.Value))

If sLocalLookup = "" Then
   Findany = -1
   Exit Function
End If
While Len(Trim(sLocalLookup)) > 0
   'extract search item from the left of the cell/string
   sTest = Trim(IIf(InStr(1, sLocalLookup, " ") > 0, Left(sLocalLookup, InStr(1, sLocalLookup, " ")), Trim(sLocalLookup)))
   'test for presence in search string
   If InStr(1, sLocalSearchStr, sTest) > 0 Then
      lMatches = lMatches + 1 ' if present
   End If
   'remove search items just checked
   sLocalLookup = Trim(Replace(sLocalLookup, sTest, ""))
   
   DoEvents
Wend
Findany = lMatches
Exit Function
End Function

and Function

=IF(Findany(A16,C16)=-1,"Empty",IF(Findany(A16,C16)>0,"Match","No-Match"))

WoW. Thanks for taking the time to do that for me.

I put the VBA portion in the code of the SHEET? Then put the formula in the cell right?
 
Upvote 0
Yes correct (this will find "Youn" in "Young" but not "Young1" in "Young")

so it may require twicking on how you want to handle spaces

possibly replace

If InStr(1, sLocalSearchStr, sTest) > 0 Then

with

If InStr(1, sLocalSearchStr, sTest & " ") > 0 Then ' ive add a space to the end of the test word

to enforce a trailing space
 
Upvote 0
Yes correct (this will find "Youn" in "Young" but not "Young1" in "Young")

so it may require twicking on how you want to handle spaces

possibly replace

If InStr(1, sLocalSearchStr, sTest) > 0 Then

with

If InStr(1, sLocalSearchStr, sTest & " ") > 0 Then ' ive add a space to the end of the test word

to enforce a trailing space

It gives me a #NAME error. not sure how to troubleshoot this one..
 
Upvote 0
=IF(A2="","Empty",IF(COUNT(SEARCH(LEFT(A2,FIND(" ",A2&" ")-1),B2)),"Match","No Match"))

That one worked as far as i can tell! thanks T. Valko

(charleschuckiecharles & JackDanIce, thanks for trying too)
You're welcome. Thanks for the feedback! :cool:

It looks like you've expanded your query!
 
Upvote 0

Forum statistics

Threads
1,216,725
Messages
6,132,345
Members
449,719
Latest member
excel4mac

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