简体   繁体   English

带有通配符的Excel区分大小写的COUNTIF:本机函数?

[英]Excel case-sensitive COUNTIF with wildcards: native function?

I'm trying to find a native Excel function (or combination thereof) which will behave precisely in the manner of COUNTIF (ie will handle wildcards), but is case-sensitive. 我正在尝试找到一个本机Excel函数(或其组合),它将以COUNTIF的方式精确运行(即将处理通配符),但区分大小写。

I have successfully used the SUMPRODUCT/EXACT functions, which do indeed perform the case-sensitive count: however the problem is that I can't get these to recognise wildcards. 我已成功使用SUMPRODUCT / EXACT函数,它确实执行区分大小写的计数:但问题是我无法识别通配符。

Perhaps it's better to give an example of what I actually need. 或许最好举一个我真正需要的例子。 I want to take a range, and search for the number of occurrences of the text "TBA", where the letters must be in capitals, but they can appear anywhere in the cell. 我想取一个范围,并搜索文本“TBA”的出现次数,其中字母必须是大写字母,但它们可以出现在单元格的任何位置。 For example: 例如:

TBA - should count TBA - 应该算一算

tbA - should not count tbA - 不应该算

somethingTBAsomething - should count somethingTBAsomething - 应该算一算

somethingtBasomething - should not count somethingtBasomething - 不应该算

=COUNTIF(A1:A10,"*TBA*")
' Correctly accounts for wildcards (*), but isn't case-sensitive

=SUMPRODUCT(--EXACT(A1:A10,"TBA"))
' Is case-sensitive, but only finds whole cell values which match

=SUMPRODUCT(--EXACT(A1:A10,"*TBA*"))
' Doesn't recognise * as a wildcard, because it's
' literally only searching for cells with asterisks
' either side of the letters

To solve my problem I have written a user-defined function as follows: 为了解决我的问题,我编写了一个用户定义的函数,如下所示:

Option Compare Binary

' Case-sensitive COUNTIF
Function CS_Countif(rng As Range, str As String) As Long
    Dim Matches As Long, cl As Range
    For Each cl In rng
        If InStr(cl.Value, str) > 0 Then Matches = Matches + 1
    Next cl
    CS_Countif = Matches
End Function

However, this function slows down the spreadsheet calculation, and there is a notable delay every time one of the cells in the relevant range is updated. 但是,此功能会减慢电子表格的计算速度,并且每次更新相关范围中的一个单元格时都会有明显的延迟。

Can anyone figure out a combination of native Excel functions (in the same manner as SUMPRODUCT/EXACT) which will do what I'm looking for? 任何人都可以找到原生Excel功能的组合(以与SUMPRODUCT / EXACT相同的方式),这将完成我正在寻找的东西吗? Thanks. 谢谢。

Since FIND is case sensitive, you can use that: 由于FIND区分大小写,您可以使用:

=COUNT(INDEX(FIND("TBA",A1:A7),))

The INDEX function is only there so you don't have to array-enter the formula. INDEX函数只在那里,所以你不必数组输入公式。

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

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