PDA

View Full Version : [SOLVED:] Search for text string withing another string



bjay
08-30-2014, 02:04 PM
Hello all;

I'm trying to find an answer for the following with no luck. I'm a beginner level programmer, and hoping someone will point me in the right direction.

I need to know if a string contains part of another string.

Eg:
strFirst = "Mid Century Modern"
strTwo = "Mid Century Modern Sofa"

As you see, both have the string "Mid Century Modern"

So, I want to write a function that will loop through several strings and check if this is true.

I have tried, InStr(), but didn't get the result I want. Also, I'm playing with the following code but cannot get this to work (although not sure if this is the way to go)


Function test()
Dim TestString As String
Dim TestString2 As String


TestString = "Mid Century Modern"
TestString2 = "Mid Century Modern Sofa"


Dim TestArray() As String
Dim TestArray2() As String


TestArray() = Split(TestString)
TestArray2() = Split(TestString2)


Dim Str As Variant
For Each Str In TestArray2
If Filter(TestArray2, Str, True) Then ' **** it gives me a type mismatch on this line****
MsgBox "found"
End If
Next


End Function


Please help! I appreciate your time. Thanks
BJ

SamT
08-30-2014, 02:12 PM
If InStr(String1, String2) > 0 Then String1 Found

bjay
08-30-2014, 06:10 PM
Wow! that simple!... thank you so much SamT, it worked. (only change I made was: If InStr(String2, String1) > 0 Then String1 Found

SamT
08-30-2014, 07:44 PM
:thumb