我在一列中列出了ID列表。有时单元格有一个id,有时单元格有多个id:
[ "52b46bfc763f4ad9198b45ab", "533c0cba763f4a505e8b46db" ] - multiple id's
52c2ff91763f4a572d8b45e6 - single id
我使用COUNTIF
来计算给定ID的所有实例。如果每个单元格中只有一个id,它就可以工作。但是,如果单元格中有多个id,则会失败。
如何计算给定实例在列中显示的次数?很高兴使用excel功能或VBA。
感谢
答案 0 :(得分:4)
使用通配符:
=COUNTIF(A:A,"*52c2ff91763f4a572d8b45e6*")
例如:
答案 1 :(得分:1)
我喜欢Gary's Student
方法。
此处参考是使用用户定义函数的VBA解决方案。例如:
A B
1 52c2ff91763f4a572d8b45e6 `=COUNTIDS(A1)` = 4
2 52c2ff91763f4a572d8b45e6, 52c2ff91763f4a572d8b45e6
3 52c2ff91763f4a572d8b45e6
4 52b46bfc763f4ad9198b45ab, 533c0cba763f4a505e8b46db
VBA模块中的此代码:
Function CountIDs(id As Range)
Dim ids As Range, cl As Range, cnt As Long, arr As Variant, i As Integer
Set ids = Range("A1:A" & Range("A1").End(xlDown).Row) //amend to suit your set-up
cnt = 0
For Each cl In ids
If InStr(1, cl, ",", vbTextCompare) Then
arr = Split(cl, ",")
For i = 0 To UBound(arr)
If VBA.Trim(arr(i)) = id Then
cnt = cnt + 1
End If
Next i
Else
If cl = id Then
cnt = cnt + 1
End If
End If
Next
CountIDs = cnt
End Function