如果条件string长度超过256个字符,则COUNTIF / SUMIF会给出错误

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

有关如何解决这个问题的任何build议? 我已经制定了一个解决scheme,我将作为一个答复张贴,但我想看看是否有其他人有一个更好的办法。

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

正如我所说,我想看看有没有人有更好的想法如何做到这一点,但我希望这有助于!

如果没有示例数据,任何build议都会涉及到一些猜测,但是听起来好像您的search条件可能会被截断为小于255个字符限制的唯一部分,并以通配符包装。

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

countif_long_string
点击查看完整大小的图片