Find duplicate text in a string and remove it

谁说胖子不能爱 提交于 2019-12-13 04:43:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!