String Comparison Help

paulcianf

New Member
Joined
Mar 6, 2014
Messages
16
CompanyCode
CompanyCodeResult
112 New York asq
442 New York abd
Pass
334 San Fran wws
252 Kansas 1saq
Fail
210 St.Louis qes
088 St.Louis abc
Pass

<tbody>
</tbody>

Hi there,
I am looking for help on creating a string for my spreadsheet. The table above is a small sample size of what I require. Essentially, if any 7 character string in column A matches column B, I need column C to show Pass. And if there is no match, it should saw Fail. So for the first example, even though the numbers and letters do not match, because it has the New York it results in a pass. Any help is appreciated.
Thanks

 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Something like this


Excel 2010
ABC
1112 New York asq442 New York abdPass
2334 San Fran wws252 Kansas 1saqFail
3210 St.Louis qes088 St.Louis abcPass
Sheet9
Cell Formulas
RangeFormula
C1{=IF(SUM(IFERROR(SEARCH(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-6)),7),B1),0))>0,"Pass","Fail")}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Something like this

Excel 2010
ABC
1112 New York asq442 New York abdPass
2334 San Fran wws252 Kansas 1saqFail
3210 St.Louis qes088 St.Louis abcPass

<colgroup><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet9

Array Formulas
CellFormula
C1{=IF(SUM(IFERROR(SEARCH(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-6)),7),B1),0))>0,"Pass","Fail")}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>

Awesome thanks so much!
 
Upvote 0
Code:
lastRow = Range("A" & Rows.Count).End(xlup).Row
i = 2
Do Until i > lastRow
    myAValue = Range("A" & i).Value
    myBValue = Range("B" & i).Value
    myALen = Len(myAValue)
    matchCounter = 0
    ii = 1
    Do Until ii > myALen
        myAChr = MID(myAValue, ii, 1)
        Do Until iii > myBLen
            myBChr = MID(myBValue, iii, 1)
            If myAChr = myBChr Then
                matchCounter = matchCounter + 1
                Exit Do
            End If
            iii = iii + 1
        Loop
        ii = ii + 1
    Loop
    If matchCounter > 7 Then
        Range("C" & i).Value = "Pass"
    Else
        Range("C" & i).Value = "Fail"
    End If
    i = i + 1
Loop
Here's how the code works.
The first Do Loop says Run this code until it evaluates ever row in the table.
The second Do Loop says Run this code until it evaluates every character in the value of the A column in the row number it is currently evaluating.
The third Do Loop says Run this code until it evaluates every character in the value of the B column in the row number it is currently evaluating.
The first If statement checks to see if the currently evaluating character of the A value matches the currently evaluating character of the B column. If it matches, it adds 1 to the counter and exits 1 Do Loop that it is in because it already found a match. The counter should be greater than 7 for it to pass.

Here's the flaw in this logic: Lets say you have a string in A2 that says "Hi Hello" and a string in cell B2 that says "Hi Jelly". Us humans can see that the second "H" in "Hi Hello" does not match the "J" in "Hi Jelly". The "o" in "Hi Hello" does not match the "y" in "Hi Jelly". So that means that there are only 6 matching characters because a space in the text counts as a character. This test should Fail, but it doesn't. And here's why. The logic I created in my code will match the first "H" in "Hi Hello" with the first "H" in "Hi Jelly" as it should. But then it will later match the second "H" in "Hi Hello" to the "H" in "Hi Jelly". This will cause the matchings to reach 7. So this test will Pass in the code I gave you. Using this logic, I can't get it any better. This is beyond me.

Now I do have another idea but I'd have to ask you something before I make the code. Lets look at your table in cells A2 that says "112 New York asq" and B2 that says "442 New York abd". New York matches New York in both columns. But what if B2 said ewN orkY? Would that be considered a match? Because it does have those characters in A2 only in different order. If they are not considered a match then I can most definately create code that looks for consecutive 7 character matches.
 
Upvote 0
How about
=IF(SUMPRODUCT(--(COUNTIF(B:B,"*"&MID(A1&REPT(CHAR(5),255),COLUMN(A1:AZ1),7)&"*")>0))>1,"pass","Fail")
 
Upvote 0
I like MM's and Mike's solutions, but I had this laying around, so ...

Row\Col
A​
B​
C​
D​
E​
1​
CompanyCode​
CompanyCode​
Common Substring​
P/F​
2​
112 New York asq442 New York abd2 New York aPC2: =LCS(A2, B2)
3​
334 San Fran wws252 Kansas 1saqanFD2: =IF(LEN(C2)>=7, "P","F")
4​
210 St.Louis qes088 St.Louis abc St.Louis P

Code:
Function LCS(ByVal s1 As String, ByVal s2 As String, _
             Optional bCaseSensitive As Boolean = False) As String
    ' shg 2009/2010
    ' Returns the Longest Common Substring in s1 and s2

    ' Max steps = Len(shorter) * (Len(shorter) + 1) / 2

    Dim iComp       As VbCompareMethod
    Dim iLen        As Long     ' match length
    Dim iBeg        As Long     ' where match begins in s1
    Dim sT          As String   ' temp string for swapping

    If bCaseSensitive Then iComp = vbBinaryCompare Else iComp = vbTextCompare
    ' swap if necessary so s1 is the longer string
    If Len(s1) < Len(s2) Then: sT = s1: s1 = s2: s2 = sT

    For iLen = Len(s2) To 1 Step -1
        For iBeg = 1 To Len(s2) - iLen + 1
            LCS = Mid(s2, iBeg, iLen)
            If InStr(1, s1, LCS, iComp) Then Exit Function
        Next iBeg
    Next iLen

    LCS = vbNullString
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,702
Members
449,048
Latest member
81jamesacct

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