识别重复项或COUNTIF> 255个字符(长文本)

时间:2018-08-04 19:17:47

标签: excel excel-2010 conditional-formatting countif

我有一个奇怪的问题,我有一个包含两列的excel文档,col A包含键,这不是唯一的,col b包含不同长度的文本,通常超过255个字符。

我想对A列应用过滤器以选择两个键,这很好,然后我想确定B列中存在的任何重复项-仅适用于可见字段,即结果列A上的过滤器。

这应该很简单;但是,我猜条件格式无法识别重复项,因为它不喜欢文本的长度。

即使尝试这种手动方式也失败了,因为搜索框似乎只能接受一定长度的搜索字符串。

1 个答案:

答案 0 :(得分:3)

COUNTIF function的字符串条件限制为255个字符。

  

来自support.office.com
长字符串返回错误的值。 当您使用COUNTIF函数匹配超过255个字符的字符串时,返回错误结果。

在COUNTIF支持页面上,support.office.com提供了一种变通解决方案,但是我无法使其正常运行,因此我编写了一个有效的用户定义函数,并添加了隐藏/可见和区分大小写的选项。

COUNTIFSBIGTXT function -长度超过255个字符的条件字符串的CountIfs功能

将其放置在公共模块代码表中(alt + F11,Insert,Module)。

Option Explicit

Function COUNTIFSBIGTXT(iOptions As Long, ParamArray pairs()) As Long
    'COUNTIFSBIGTXT - CountIfs functionality for criteria strings longer than 255 characters
    ' https://stackoverflow.com/questions/51688846#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

Syntax:

=COUNTIFSBIGTXT(<options>, <string_range1>, <criteria1>, [optional string_range2], [optional criteria2], …)

Documentation

enter image description here

Sample data ²

以下示例基于相同的600字符串的9行,其中三行被强制为大写。

enter image description here

Example 1

简单的COUNTIF操作会丢弃过滤/隐藏的行,并且不区分大小写。

=COUNTIFSBIGTXT(0, B:B, B2)

enter image description here

Example 2

扩展的COUNTIFS¹操作将丢弃已过滤/隐藏的行,但区分大小写的字符串比较。次要条件为A:A等于A2中的值。

=COUNTIFSBIGTXT(2, B:B, B2, A:A, A2)

enter image description here


¹失败时逻辑流程“短路”。使用多个字符串/条件对,您可以通过首先列出最不可能的string_range / criteria匹配来提高性能
。通过针对您的特殊要求重新排列后三个'Kick-out'Exit For语句,您可能会看到特殊情况下的计算效率得到类似的提高,但是我建议保留工作表错误检查作为主要检查。
例如,如果您有很多潜在的字符串匹配项,但可见行很少,则将可见单元格的检查移到字符串匹配项检查的上方会减少条件检查。

²非常感谢Lorem Ipsum Generator提供的示例字符串内容。