title: Excel VBA Weighted Average If Custom Function
body: <p>I am trying to write a custom function that will check 2 criteria in a range then do a weighted average of any matches it finds. My code is below and it is not interpreting my criteria correctly. It's displaying a #Value on my Criteria. Any help would be greatly appreciated! Thank you :D</p>
  
  <p><a href="https://drive.google.com/file/d/0B6aefGNRPaHiRlR4a3Vvc3RNamM/edit?usp=sharing" rel="nofollow">https://drive.google.com/file/d/0B6aefGNRPaHiRlR4a3Vvc3RNamM/edit?usp=sharing</a></p>
  
  <pre><code>Function WeightedAverageIf(Range As Range, Citeria1 As String, Column1 As Integer, Criteria2 As String, Column2 As Integer, Column3 As Integer, Column4 As Integer)
       lr = Range.Cells(Rows.Count, 1).End(xlUp).Row
       Dim num1 As Integer, per1 As Integer, num2 As Integer, per2 As Integer, counter As Integer
       coutner = 0
       For x = 1 To lr
          If counter = 0 Then
             If Range.Cells(x, Column1) = Criteria1 And Range.Cells(x, Column2) = Criteria2 Then num1 = Cells(x, Column3) And per1 = Range.Cells(x, Column4) And counter = counter + 1
             End If
          If counter &gt; 0 Then
             If Range.Cells(x, Column1) = Criteria1 And Cells(x, Column2) = Criteria2 Then num2 = Range.Cells(x, Column3) And per2 = Range.Cells(x, Column4) And counter = counter + 1
             per1 = (((per1 * num1) + (per2 * num2)) / (num1 + num2))
             num1 = (num1 + num2)
          End If
       Next x
       WeightedAverageIf = per1
  End Function
  </code></pre>