比较Excel VBA中两个单元格中的文本

时间:2016-12-29 17:33:27

标签: excel-vba vba excel

我是VBA编程的新手,希望有人可以帮忙解决这个问题。我一直在阅读其他一些帖子,但在我的水平上对我来说太复杂了。

我正在尝试比较Excel中的两个单元格并尝试匹配两个单元格中的单词,并查看它们之间是否存在任何匹配的单词。 (see picture for reference)

1 个答案:

答案 0 :(得分:0)

试试这个小 UDF()

Public Function WhatsInCommon(A As String, B As String) As String
    arya = Split(LCase(A), " ")
    aryb = Split(LCase(B), " ")

    WhatsInCommon = ""
    For Each aa In arya
        For Each bb In aryb
            If aa = bb Then
                If WhatsInCommon = "" Then
                    WhatsInCommon = aa
                Else
                    WhatsInCommon = WhatsInCommon & "," & aa
                End If
            End If
        Next bb
    Next aa
End Function

例如:

enter image description here

注意:

  1. 假设“word”是由空格
  2. 封装的字符串
  3. 转换为小写
  4. 修改#1:

    此版本仅考虑大于两个字符的单词:

    Public Function WhatsInCommon(A As String, B As String) As String
        arya = Split(LCase(A), " ")
        aryb = Split(LCase(B), " ")
    
        WhatsInCommon = ""
        For Each aa In arya
            If Len(aa) > 2 Then
                For Each bb In aryb
                    If aa = bb Then
                        If WhatsInCommon = "" Then
                            WhatsInCommon = aa
                        Else
                            WhatsInCommon = WhatsInCommon & "," & aa
                        End If
                    End If
                Next bb
            End If
        Next aa
    End Function