问题
I have this excel problem. I am trying to find matching text between 2 columns and then remove the matched text. Example
Column 1:
John Romeo
Column 2:
John Romeo 16 Smith Street
Results:
16 Smith Street
The Results column is the text that I want.
回答1:
Here's a custom function called WORDDIF that may do what you want.
To install the custom function in Windows ... Alt+F11 to open the VBA Editor From the VBA menu, select Insert -> Module
To install the custom function in OS X ... Go to Tools -> Macro -> Visual Basic Editor From the VBA menu, select Insert -> Module
Paste the code below in the VBA Edit window
Back in Excel, copy this formula into your Results column:
Results=WORDDIF(Column1Cell1, Column2Cell2)
Function WORDDIF(rngA As Range, rngB As Range) As String
Dim WordsA As Variant, WordsB As Variant
Dim ndxA As Long, ndxB As Long, strTemp As String
WordsA = Split(rngA.Text, " ")
WordsB = Split(rngB.Text, " ")
For ndxB = LBound(WordsB) To UBound(WordsB)
For ndxA = LBound(WordsA) To UBound(WordsA)
If StrComp(WordsA(ndxA), WordsB(ndxB), vbTextCompare) = 0 Then
WordsA(ndxA) = vbNullString
Exit For
End If
Next ndxA
Next ndxB
For ndxA = LBound(WordsA) To UBound(WordsA)
If WordsA(ndxA) <> vbNullString Then strTemp = strTemp & WordsA(ndxA) & " "
Next ndxA
WORDDIF = Trim(strTemp)
End Function
For attribution, the solution is from http://www.mrexcel.com/forum/excel-questions/486708-compare-two-strings-find-difference.html
来源:https://stackoverflow.com/questions/22950762/find-duplicate-text-in-a-string-and-remove-it