title: Create VBA code for MAXifs
body: <p>I have tried to adapt code from another post into something easier for me to understand. When running the code, I still get an error "Type mismatch" for this line: <code>w(k) = z(i, 1)</code>. Does anyone have any insight into this error? </p>
  
  <p><strong>My Code</strong></p>
  
  <pre><code>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            
  </code></pre>