[英]Excel: Count specific words in a cell
我正在嘗試計算單元格中的特定單詞並顯示總數。 我已經搜索並嘗試了幾個類似問題的解決方案,但沒有。 在下面的示例中,此處的列A具有文本,列B具有需要計數的唯一單詞(搜索條件),列C是計數。 請幫忙
例:
| Column A | Column B | Column C |
| AA; BB; CC; AE; DE | AA; DE; CC | 3 |
考慮:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.