Joh*_*ohn 3 excel conditional-formatting countif excel-2010
我有一个奇怪的问题,我有一个包含两列的 excel 文档,col A 包含键,这不是唯一的,col b 包含不同长度的文本,通常在 255 个字符以上。
我想对 A 列应用过滤器以选择两个键,这很好,然后我想识别 B 列中存在的任何重复项 - 仅适用于可见的字段,即作为过滤器的结果在 A 列上。
这应该很简单;但是,条件格式无法识别重复项,我猜是因为它不喜欢文本的长度。
即使尝试这种 mandrolic 方式也失败了,因为搜索框似乎只能接受一定长度的搜索字符串。
小智 8
该COUNTIF函数为255个字符的字符串的标准的限制。
来自support.office.com
为长字符串返回了错误的值。 当您使用 COUNTIF 函数匹配长度超过 255 个字符的字符串时,它会返回不正确的结果。
support.office.com 在 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
' /sf/ask/3618219251/#51689459
'
' =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
Run Code Online (Sandbox Code Playgroud)
=COUNTIFSBIGTXT(<options>, <string_range1>, <criteria1>, [optional string_range2], [optional criteria2], …)
Run Code Online (Sandbox Code Playgroud)
样本数据²
以下示例基于相同的 600 个字符串的 9 行,其中 3 行强制为大写。
简单的 COUNTIF 操作丢弃过滤/隐藏的行并且不区分大小写。
=COUNTIFSBIGTXT(0, B:B, B2)
Run Code Online (Sandbox Code Playgroud)
扩展的 COUNTIFS¹ 操作丢弃过滤/隐藏的行,但区分大小写的字符串比较。带有 A:A 的次要条件等于 A2 中的值。
=COUNTIFSBIGTXT(2, B:B, B2, A:A, A2)
Run Code Online (Sandbox Code Playgroud)
¹逻辑流程在失败时“短路”。使用多个字符串/标准对,您可以
通过首先列出最不可能的 string_range/标准匹配来提高性能。
通过根据您的
特定要求重新排列后三个“踢出”Exit For 语句,您可能会看到特殊情况下计算效率的类似提高,但我建议将检查工作表错误作为主要检查。
例如,如果您有很多潜在的字符串匹配,但可见行很少,将
可见单元格检查移到字符串匹配检查上方会减少条件检查。
²非常感谢Lorem Ipsum Generator提供了示例字符串内容。
| 归档时间: |
|
| 查看次数: |
2934 次 |
| 最近记录: |