如果条件字符串超过256个字符,则COUNTIF / SUMIF会出错

时间:2016-07-01 20:51:33

标签: excel countif sumifs

在尝试将COUNTIF和SUMIF与经常有长注释的表一起使用时,我不断收到#VALUE错误。一些研究表明,错误可能是由于标准字符串超过256个字符点。

有关如何解决此问题的任何建议?我已经找到了一个我将作为答案发布的解决方案,但我想看看是否有其他人有更好的方法。

2 个答案:

答案 0 :(得分:2)

我最终在VB中编写了一对UDF来解决这个问题。还有一个字符限制,但现在它是2 ^ 32,而不是2 ^ 8。

COUNTIF的变化非常简单......

    Function COUNTIFLONG(rng As Range, crt As String, ExactMatch As Boolean)

    Dim Cell As Range
    Dim x As Integer

    x = 0

    For Each Cell In rng
        If IsNull(Cell.Value) Then GoTo CellCont
        If ExactMatch Then
          If Cell.Value = crt Then
            x = x + 1
          End If
          Else
            If (InStr(Cell.Value, crt) > 0) Then
              x = x + 1
            End If
        End If
CellCont:
    Next Cell

    COUNTIFLONG = x

End Function

SUMIF的变化对于让它足够灵活以便经常使用来说有点棘手。

 Function SUMIFLONG(rngCrt As Range, crt As String, rngSum As Range, ExactMatch As Boolean)

    Dim Cell As Range
    Dim x As Integer
    Dim CrtRows As Integer, CrtCols As Integer, SumRows As Integer, SumCols As Integer
    Dim RowOffset As Integer, ColOffset As Integer
    Dim SumDir As String

    CrtRows = rngCrt.Rows.Count
    CrtCols = rngCrt.Columns.Count
    SumRows = rngSum.Rows.Count
    SumCols = rngSum.Columns.Count

    crt = Trim(crt)

    x = 0

    If (CrtRows <> SumRows) Or (CrtCols <> SumCols) Then
        Debug.Print ("Arrays are not the same size.  Please review the formula.")
        Exit Function
    End If

    If (CrtRows <> 1) And (CrtCols <> 1) And (SumRows <> 1) And (SumCols <> 1) Then
        Debug.Print ("Please restrict arrays to one column or row at a time.")
        Exit Function
    End If

    'Detects the offset of the Sum row/column from the Criteria row/column
    RowOffset = rngSum.Row - rngCrt.Row
    ColOffset = rngSum.Column - rngCrt.Column

    For Each Cell In rngCrt
    'Ignores Null cells or rows where the Sum column's value is not a number.
        If IsNull(Cell.Value) Or (Not IsNumeric(Cell.Offset(RowOffset, ColOffset).Value)) Then
          GoTo CellCont
        End If

    'Adds Sum Column's value to the running total.
    'If an Exact Match is not requested, will detect whether Criteria is present in target cell.
        If ExactMatch Then
          If Cell.Value = crt Then
            x = x + Cell.Offset(RowOffset, ColOffset).Value
          End If
          Else
            If (InStr(Cell.Value, crt) > 0) Then
              x = x + Cell.Offset(RowOffset, ColOffset).Value
            End If
        End If
 CellCont:
    Next Cell

    SUMIFLONG = x

 End Function

正如我所说,我想看看是否有人有更好的想法如何实现这一点,但我希望这有帮助!

答案 1 :(得分:2)

如果没有样本数据,任何建议都会涉及一些猜测,但听起来您的搜索条件可能会被切割成小于255个字符限制的独特部分并包装在通配符中。

=COUNTIF(A:A, "*"&C2&"*")

countif_long_string
点击查看完整尺寸的图片