2016-07-01 3 views
1

COUNTIFとSUMIFを定期的に長いコメントを持つテーブルで使用しようとしても、#VALUEエラーが発生しました。少しの研究では、エラーは256文字ポイントを上回る基準文字列に起因する可能性があると述べています。COUNTIF/SUMIFは、基準文字列が256文字より長い場合にエラーを返します

これを回避する方法についてのご意見はありますか?私は回答として投稿する解決策を見つけましたが、他の誰かがより良い方法を持っているかどうかを確認したいと思います。

答えて

2

問題を回避するために、VBでUDFのペアを作成しました。まだ文字数制限がありますが、今は2^8ではなく2^32です。

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 

私が言ったように、私は誰もがこれを実現する方法のより良いアイデアを持っていたかどうかを確認したいのですが、私はこのことができます願っています!

+0

のClick、私はむしろ、もし'より 'ExactMatch'をテストするcase'文を選択し、'使用したいと思います...その後、 ... else'ステートメント。 – PaichengWu

+0

よろしくお願いします。ありがとう!私はある時点でそれを更新します。私はCASEをより頻繁に使用する習慣に入る必要があります... – TesseractE

2

サンプルデータがないと、推測が必要になりますが、検索条件が255文字以下のワイルドカードで囲まれているように見えます。

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

countif_long_string
COUNTIFLONG`ユーザー定義関数 `でフルサイズのイメージ

+0

ええ、私が検索しなければならなかった部分文字列は、時には長すぎることもありました。機密情報なのでサンプルデータを提供することはできませんが、サンプルは私が扱っていたもののスタイルにかなり近いものです。 – TesseractE

関連する問題