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

问题描述:

我是VBA编程新手,希望能有人帮助解决这个问题。我一直在阅读其他一些帖子,但对我来说太复杂了,无法理解我的水平。比较Excel中两个单元格内的文本VBA

我想比较Excel中的两个单元格,并试图匹配这两个单元格中的单词,看看是否有任何单词匹配它们之间。 (see picture for reference)

+0

请回顾[如何提问](http://*.com/help/how-to-ask)在本网站上获得最佳帮助。 –

尝试这个小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. 假定一个 “字”

    这个版本只会考虑的话多于两个字符:是一串字符用空格

  2. 转换为小写

编辑#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 
+0

我添加到我的文件,完全正常工作,我需要什么。非常感谢...一个问题?有什么办法来限制搜索> 2个字符。我想要消除这个功能,让我知道或者,是,在(对于更大的文本)。感谢支持! – manny

+0

@manny请参阅我的*编辑#1 ** –

+0

谢谢。作品完美无瑕.... – manny