简体   繁体   English

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

[英]Excel: Count specific words in a cell

I am trying to count specific words in cell and display the total. 我正在尝试计算单元格中的特定单词并显示总数。 I have searched and tried solutions of several similar questions but couldn't. 我已经搜索并尝试了几个类似问题的解决方案,但没有。 Here in the example below Column A has the text, Column B has unique words (criteria for search) that need to be counted and Column C is the count. 在下面的示例中,此处的列A具有文本,列B具有需要计数的唯一单词(搜索条件),列C是计数。 Please help 请帮忙

Example: 例:

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

Consider: 考虑:

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

For example: 例如:

在此处输入图片说明

If a word like AA appeared more than once in cell A1 , the function would count each occurrence . 如果像AA这样的单词在单元格A1中出现多次,该函数将计算每次出现次数。

EDIT#1: 编辑#1:

This version will work with or without spaces: 此版本可以使用空格,也可以不使用空格:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM