我正在尝试计算单元格中的特定单词并显示总数。我已经搜索并尝试了几个类似问题的解决方案,但无法解决。在下面的示例中,列A具有文本,列B具有需要计数的唯一字(搜索条件),而列C是计数。请帮忙
示例:
| Column A | Column B | Column C |
| AA; BB; CC; AE; DE | AA; DE; CC | 3 |
答案 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
例如:
如果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