2016-12-08 45 views
1

別の投稿のコードを理解しやすくなるように修正しました。コードを実行するとき、私はまだこの行のために "タイプ不一致"というエラーを受け取ります:w(k) = z(i, 1)。誰もがこのエラーの洞察を持っていますか?MAXIF用のVBAコードを作成する

マイコードあなたはMaxRange範囲の間で選択するために、いくつかのうちの最大値を返すに興味があるので、

Option Base 1 

Function MaxIf(MaxRange As Range, Lookup_Range1 As Range, Var_Range1 As Variant, _ 
       Lookup_Range2 As Range, Var_Range2 As Variant) As Variant 

    Dim x() As Variant, y() As Variant, z() As Variant, w() As Long 
    Dim i As Long 
    Dim Constraint1 As Variant, Constraint2 As Variant, k As Long 

    i = 1 
    k = 0 
    Constraint1 = Var_Range1 
    Constraint2 = Var_Range2 
    x = Lookup_Range1 
    y = Lookup_Range2 
    z = MaxRange 

    For i = 1 To Lookup_Range1.Rows.Count 
     If x(i, 1) = Var_Range1 Then 
      If y(i, 1) = Var_Range2 Then 
       k = k + 1 
       ReDim Preserve w(k) 
       w(k) = z(i, 1) 
      End If 
     End If 
    Next i 
    MaxIf = Application.Max(w) 

End Function    
+4

エラーが発生したときの 'z(i、1)'の値は何ですか?私の推測では、 'String'、エラー、または暗黙的に' Long'にキャストできない他のデータ型が含まれていると思います。エラー行のすぐ上に 'Debug.Assert IsNumeric(z(i、1))'行を追加してチェックすることができます。 – Comintern

+0

@Diedrichあなたの 'Function'が何をしたいのかを説明しようとしていますか?ワークシートのスクリーンショットを追加して、期待される結果が得られるかもしれません。 –

+0

他の列の対応するセルが特定の基準と一致すると、範囲内の最大値を取得しようとしていると思います。あなたは 'MAX( - (Range1 = Criteria1)* - (Range2 = Criteria2)* MaxRange)'のような簡単な公式でこれを達成することができます。 'Max( - (A1:A15 =" John ")* - (B1:B15 = 20)* C1:C15)' – nightcrawler23

答えて

0

、その後、あなただけの数値値をループし、対応するの状況を確認することができLookup_Range1のみLookup_Range2の細胞、次のように:

Function MaxIF(MaxRange As Range, Lookup_Range1 As Range, Var_Range1 As Variant, _ 
       Lookup_Range2 As Range, Var_Range2 As Variant) As Variant 

    Dim LU1 As Variant, LU2 As Variant 
    Dim founds As Long 
    Dim cell As Range 

    LU1 = Lookup_Range1.Value2 '<--| store Lookup_Range1 values 
    LU2 = Lookup_Range2.Value2 '<--| store Lookup_Range2 values 

    ReDim ValuesForMax(1 To MaxRange.Rows.count) As Long '<--| initialize ValuesForMax to its maximum possible size 
    For Each cell In MaxRange.Columns(1).SpecialCells(xlCellTypeConstants, xlNumbers) 
     If LU1(cell.row, 1) = Var_Range1 Then '<--| check 'Lookup_Range1' value in corresponding row of current 'MaxRange' cell 
      If LU2(cell.row, 1) = Var_Range2 Then '<--| check 'Lookup_Range2' value in corresponding row of current 'MaxRange' cell 
       founds = founds + 1 
       ValuesForMax(founds) = CLng(cell) '<--| store current 'MaxRange' cell 
      End If 
     End If 
    Next cell 
    ReDim Preserve ValuesForMax(1 To founds) '<--| resize ValuesForMax to its actual values number 
    MaxIF = Application.max(ValuesForMax) 
End Function 

Iも変数に意味のある名前を与えました

+0

助けてくれてありがとう。 user3598756、あなたのコードを実行しているときにランタイムエラー9 "subscript out of range"エラーが表示される:If LU1(cell.Row、1)= Var_Range1 Then '< - |現在の 'MaxRange'セルの対応する行の 'Lookup_Range1'値をチェックしてください。 これを引き起こしている原因は分かりますか? – Diedrich

+0

関数に渡した3つの範囲は何ですか? – user3598756

+0

これらはすべて最終行を使用して定義された範囲です。 (ワークシート(データ))セル(2、定義された列)、ワークシート( "データ")セル(最後の行、定義された列))、私は今実際のコードを提供することができません、私は現在仕事中で、自分のパーソナルコンピュータ/ファイルにアクセスすることはできません。私は今日後で実際の範囲を送ることができます。再度、感謝します! – Diedrich

0

コードを実行すると、2つの条件に制限されていました。 MaxIfs関数の条件数を制限しないために、このコードをさらに使うことにしました。以下のコードを参照してください:

Function MaxIfs(MaxRange As Range, ParamArray Criteria() As Variant) As Variant 
    Dim n As Long 
    Dim i As Long 
    Dim c As Long 
    Dim f As Boolean 
    Dim w() As Long 
    Dim k As Long 
    Dim z As Variant 

    'Error if less than 1 criteria 
    On Error GoTo ErrHandler 
    n = UBound(Criteria) 
    If n < 1 Then 
     'too few criteria 
     GoTo ErrHandler 
    End If 

    'Define k 
    k = 0 

    'Loop through cells of max range 
    For i = 1 To MaxRange.Count 

    'Start by assuming there is a match 
    f = True 

     'Loop through conditions 
     For c = 0 To n - 1 Step 2 

      'Does cell in criteria range match condition? 
      If Criteria(c).Cells(i).Value <> Criteria(c + 1) Then 
       f = False 
      End If 

     Next c 

     'Define z 
     z = MaxRange 

     'Were all criteria satisfied? 
     If f Then 
      k = k + 1 
      ReDim Preserve w(k) 
      w(k) = z(i, 1) 
     End If 

    Next i 

    MaxIfs = Application.Max(w) 

    Exit Function 
    ErrHandler: 
    MaxIfs = CVErr(xlErrValue) 

End Function 

このコードでは、複数の条件が可能です。

このコードは、Eileen's LoungeでHans Vが投稿した複数のコードを参照して開発されたものです。

Diedrich

関連する問題