我是VBA编程的新手,希望有人可以帮忙解决这个问题。我一直在阅读其他一些帖子,但在我的水平上对我来说太复杂了。
我正在尝试比较Excel中的两个单元格并尝试匹配两个单元格中的单词,并查看它们之间是否存在任何匹配的单词。 (see picture for reference)
答案 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
例如:
注意:
修改#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