在字符串中查找重复文本并将其删除

时间:2014-04-09 00:36:15

标签: excel match trim vlookup duplicate-data

我有这个excel问题。我试图找到2列之间的匹配文本,然后删除匹配的文本。实施例

第1栏:

John Romeo

第2栏:

John Romeo 16 Smith Street

结果:

16 Smith Street 

“结果”列是我想要的文本。

1 个答案:

答案 0 :(得分:-1)

这是一个名为WORDDIF的自定义函数,可以执行您想要的操作。

在Windows中安装自定义功能... Alt + F11打开VBA编辑器 从VBA菜单中,选择插入 - >模块

在OS X中安装自定义功能... 转到工具 - >宏 - > Visual Basic编辑器 从VBA菜单中,选择插入 - >模块

将以下代码粘贴到VBA编辑窗口

返回Excel,将此公式复制到“结果”列中:

结果= 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

对于归因,解决方案来自http://www.mrexcel.com/forum/excel-questions/486708-compare-two-strings-find-difference.html