excel - 如果条件字符串长于 256 个字符,COUNTIF/SUMIF 会给出错误

标签 excel countif sumifs

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

关于如何解决这个问题有什么建议吗?我已经制定了一个解决方案,我将作为答案发布,但我想看看是否还有其他人有更好的方法。

最佳答案

我最终在 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

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

关于excel - 如果条件字符串长于 256 个字符,COUNTIF/SUMIF 会给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38153720/

相关文章:

excel - 如何像范围一样定义二维数组的列?

excel - 用于练习的基于时间的 VBA 脚本

r - R 中的 sumif 和 countif 等效项

javascript - 使用谷歌脚本达到一定数量时显示值

c# - 是否有用于访问Excel文件的免费API?

excel - 导入单元格的值以用于数组中的文本到列分隔时,出现类型不匹配错误

google-sheets - Countifs 出错,函数任何帮助表示赞赏

Excel - 多列的 SUMIFS

excel - 拖动时更新公式行和列

excel - 如果 y 字符串包含 a 或 b 或 c,则求和 x - Excel