Hi,
Since I wasn't sure how many words to compare from your sample data, added 3rd argument to determine.
e.g
in cell
=wfind(A1:B1:B10,3)
The code will:
go through B1 to B10 testing if there is a cell which matches number of words from the left.
if A1 contains AA BBB CCC nn (will test up to CCC, since the 3rd arg is 3)
B1 AA BBB nn (not found)
B2 AA BBB CCC DDD FFF (found)
B3 AA CCC BBB nn (not found)
B4 AA BBB CCC dd (found)
B5 BB AAA CCC (not found)
B6 CC BBB AAA dd (not found)
B7 AAA BBB CCC QQ (found)
.
.
.
Function wfind(r As Range, rng As Range, Optional cap As Integer) As String
Dim c As Range, txt1, txt2, flag As Boolean, i As Integer
txt1 = Split(r, " "): flag = False
' **** put the testing string into array txt1 by separating by space ***
If IsMissing(cap) Then
' **** if cap, 3rd argument, is missing ****
cap = UBound(txt1)
' **** set cap = upperbound of txt1, which means the number of words
' separated by the spaces *****
Else
cap = cap - 1
End If
For Each c In rng
txt2 = Split(c, " ")
' **** put the string into array txt2 by separating by space ****
For i = LBound(txt1) To cap
If Trim(txt1(i)) Like Trim(txt2(i)) & "*" Then
' **** if ith string in txt1 array is Like ith string in txt2 array then set flag=true**
flag = True
Else
' **** if ith string of txt1 array and txt2 array differ then set flag=false and exit loop***
flag = False: Exit For
End If
If i = UBound(txt2) Then Exit For
Next
' **** after loop, if flag is still true, returns "Found" and exit function ***
If flag = True Then
wfind = r & " Found": Exit Function
End If
Next
' **** else, returns "not found" and end function ****
wfind = r & " not found"
End Function

Originally Posted by
PLPE
Hi Jindon,
Thanks for the input on my problem. I've looked at your code & it seems to return "Found" for every input. For example; I typed "hello world" in C1 and used your code to find it in a range (where it did not exist), & it returned "hello world Found".
Any chance of adding a few comments to the code so I can see where you are coming from?
Thanks,
Jerry
Bookmarks