Excel:计算单元格中的特定单词

时间:2017-10-08 02:30:40

标签: excel excel-formula

我正在尝试计算单元格中的特定单词并显示总数。我已经搜索并尝试了几个类似问题的解决方案,但无法解决。在下面的示例中,列A具有文本,列B具有需要计数的唯一字(搜索条件),而列C是计数。请帮忙

示例:

|     Column A          |     Column B      |   Column C    |
|  AA; BB; CC; AE; DE   |    AA; DE; CC     |      3        |

1 个答案:

答案 0 :(得分:0)

考虑:

Public Function StringCounter(sBig As String, sKeys As String) As Long

ary = Split(sBig, ";")
bry = Split(sKeys, ";")
StringCounter = 0

For Each b In bry
    If InStr(1, ";" & sBig & ";", ";" & b & ";") <> 0 Then
        StringCounter = StringCounter + UBound(Filter(ary, b)) + 1
    End If
Next b
End Function

例如:

enter image description here

如果AA这样的单词在单元格 A1 中出现多次,则该函数会计算每次出现的次数。

修改#1:

此版本可以使用或不使用空格:

Public Function StringCounter(sBig As String, sKeys As String) As Long

    sBig = Replace(sBig, " ", "")
    sKeys = Replace(sKeys, " ", "")

    ary = Split(sBig, ";")
    bry = Split(sKeys, ";")
    StringCounter = 0

    For Each b In bry
        If InStr(1, ";" & sBig & ";", ";" & b & ";") <> 0 Then
            StringCounter = StringCounter + UBound(Filter(ary, b)) + 1
        End If
    Next b
End Function

enter image description here