Excel 计算单元格值为“A,B”、“R,A”等的“A”的数量

Excel 计算单元格值为“A,B”、“R,A”等的“A”的数量

尝试计算 Excel 中单个变量的数量,但 Countif 似乎不符合我的需要

例如,单个单元格有“A”、“A,B”、“A,J”等,我想计算“A”的数量 - 有没有办法在不输入所有可能性的情况下做到这一点?

答案1

COUNTIF()支持通配符:

=COUNTIF(B2:Q2,"*A*")

答案2

下面的自定义函数应该可以解决您的问题。上面的答案仅测试字符串中是否存在“A”,而不是多个“A”。例如,“AAAA”仅返回 1,而不是 4。

您可以查看此链接了解更多详细信息https://stackoverflow.com/questions/19481175/general-purpose-udfs-for-using-regular-expressions-in-excel

Public Function RXCount(Text As String, Pattern As String, Optional IgnoreCase As Boolean = True) As Integer
    Dim retval As Integer
    ' Counts the number of matches
    ' Text is the string to be searched
    ' Pattern is the regex pattern
    ' IgnoreCase (optional) set to False for a case-sensitive search

    Dim RE As Object
    Dim Matches As Object

    Set RE = CreateObject("vbscript.regexp")
    RE.IgnoreCase = IgnoreCase
    RE.Global = True

    RE.Pattern = Pattern
    Set Matches = RE.Execute(Text)

    retval = Matches.Count

    RXCount = retval
End Function

相关内容