比较Excel中两个单元格内的文本VBA
问题描述:
我是VBA编程新手,希望能有人帮助解决这个问题。我一直在阅读其他一些帖子,但对我来说太复杂了,无法理解我的水平。比较Excel中两个单元格内的文本VBA
我想比较Excel中的两个单元格,并试图匹配这两个单元格中的单词,看看是否有任何单词匹配它们之间。 (see picture for reference)
答
尝试这个小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
请回顾[如何提问](http://stackoverflow.com/help/how-to-ask)在本网站上获得最佳帮助。 –