【问题标题】:Excel: Count specific words in a cellExcel:计算单元格中的特定单词
【发布时间】:2017-10-08 12:06:31
【问题描述】:

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

例子:

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

【问题讨论】:

    标签: excel excel-formula


    【解决方案1】:

    考虑:

    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
    

    例如:

    如果像 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
    

    【讨论】:

    • 好极了,效果很好。但是,有一个问题 - 如果一个单元格在 A 列中有一个值 AE,而 B 列有 AA; AE; CC C 列中显示的计数为 0。可能是因为分号或空格?
    • @user7357285 我对代码做了一个小更新...请重新复制它。
    • @user7357285 我需要进行其他更改以容纳空间。
    • 我在您的代码中添加了空格,它运行良好。我会将问题标记为已回答。 5 星给你 - 非常感谢
    • @user7357285 查看我的EDIT#1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2019-08-03
    相关资源
    最近更新 更多