【问题标题】:Compare text within two Cells in Excel VBA在 Excel VBA 中比较两个单元格中的文本
【发布时间】:2016-12-30 04:24:15
【问题描述】:

我是 VBA 编程的新手,希望有人能提供帮助。我一直在阅读其他一些帖子,但太复杂了,以我的水平无法理解。

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

【问题讨论】:

  • 请查看How to Ask 以获得本网站未来的最佳帮助。

标签: excel vba


【解决方案1】:

试试这个小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. 假设“单词”是由空格封装的字符串
  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

【讨论】:

  • 我已添加到我的文件中,并且可以完美地满足我的需要。非常感谢... 一个问题?有什么方法可以限制搜索 >2 个字符。我想消除给我 or, is, in, on 的功能(对于较大的文本)。感谢支持!!
猜你喜欢
  • 1970-01-01
  • 2011-07-20
  • 2019-12-22
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多