COUNTIF函数的字符串条件限制为255个字符。
来自support.office.com
长字符串返回错误值。 当您使用COUNTIF函数匹配超过255个字符的字符串时,它会返回不正确的结果。
COUNTIF支持页面上提供了一种解决方法,但我无法使其工作,所以我编写了一个用户定义的函数,并添加了隐藏/可见和区分大小写选项。
COUNTIFSBIGTXT函数 - 用于超过255个字符的条件字符串的CountIfs功能
将此放入公共模块代码表中(alt+F11,插入,模块)。
Option Explicit
Function COUNTIFSBIGTXT(iOptions As Long, ParamArray pairs()) As Long
'COUNTIFSBIGTXT - CountIfs functionality for criteria strings longer than 255 characters
' https://dev59.com/J63la4cB1Zd3GeqPMWgF
'
' =COUNTIFSBIGTXT(<options>, <string_range1>, <criteria1>, [string_range2], [criteria2], …)
' OPTIONS
' 0 No options
' +1 Include hidden cells in <string_range1>, [string_range2], etc
' +2 Case sensitive comparison
'throw error if string_range and criteria do not come in pairs
If Not CBool(UBound(pairs) Mod 2) Then
COUNTIFSBIGTXT = CVErr(xlErrValue)
Exit Function
End If
'declare variables
Dim i As Long, j As Long
Dim bIncludeHidden As Boolean, bCaseSensitive As Boolean
'set optional booleans
bIncludeHidden = CBool(1 And iOptions)
bCaseSensitive = CBool(2 And iOptions)
'restrict full column references to the parent worksheet's UsedRange
Set pairs(LBound(pairs)) = Intersect(pairs(LBound(pairs)), pairs(LBound(pairs)).Parent.UsedRange)
'resize all <string_range> to the same dimensions
With pairs(LBound(pairs))
For i = LBound(pairs) + 2 To UBound(pairs) Step 2
Set pairs(i) = pairs(i).Resize(.Rows.Count, .Columns.Count)
'Debug.Print pairs(i).Address(0, 0)
Next i
End With
'loop cell count in pairs(LBound(pairs)) for relative ordinal
For i = 1 To pairs(LBound(pairs)).Cells.Count
'loop through each pair of <string_range> and <criteria>
For j = LBound(pairs) To UBound(pairs) Step 2
'exit for if any argument pair does not meet criteria
With pairs(j).Cells(i)
'throw out worksheet error codes
If IsError(.Value) Then Exit For
'do the pair(s) meet a case insensitive match
If LCase(.Value2) <> LCase(pairs(j + 1)) Then Exit For
'do the pair(s) meet a case sensitive match with option
If .Value2 <> pairs(j + 1) And LCase(.Value2) = LCase(pairs(j + 1)) And bCaseSensitive Then Exit For
'are the cells visible or hidden with include option
If (.EntireRow.Hidden Or .EntireColumn.Hidden) And Not bIncludeHidden Then Exit For
End With
Next j
'determine if all argument pairs matched
If j > UBound(pairs) Then _
COUNTIFSBIGTXT = COUNTIFSBIGTXT + 1
Next i
End Function
语法:
=COUNTIFSBIGTXT(<options>, <string_range1>, <criteria1>, [optional string_range2], [optional criteria2], …)
文档
![enter image description here](https://istack.dev59.com/34BP2.webp)
示例数据²
下面的示例是基于一个相同的600个字符字符串的9行,其中有三行被强制转换为大写。
![enter image description here](https://istack.dev59.com/bkqYB.webp)
示例1
简单的COUNTIF操作,可以丢弃过滤/隐藏的行,并且不区分大小写。
=COUNTIFSBIGTXT(0, B:B, B2)
![enter image description here](https://istack.dev59.com/QDVxU.webp)
示例2
扩展的COUNTIFS¹操作,可以丢弃筛选/隐藏的行,但是字符串比较区分大小写。第二个条件为A:A等于A2中的值。
=COUNTIFSBIGTXT(2, B:B, B2, A:A, A2)
![enter image description here](https://istack.dev59.com/8C1rh.webp)
"The logic flow 'short-circuits' on fail. With multiple string/criteria pairs, you can enhance performance by listing the least likely string_range/criteria matches first. You might see similar increases in calculation efficiency for special circumstances by reordering the latter three 'kick-out' Exit For statements for your particular requirements but I recommend keeping the check for worksheet errors as the primary check. For example, if you have a lot of potential string matches but very few visible rows, moving the check for visible cells above the check for string matches would cut down on the criteria checks."
"Many thanks to the Lorem Ipsum Generator for the sample string content."