I have recently been lumped with quite a bit of Excel based tasks to do at work, and there is one function that I keep needing that does not appear to be in VBA. "AverageIf"
Knowing that you can perform the equivelant of an AverageIf with combination of SumIf() and CountIf(), I thought I'd wrap these up in a function.
'Purpose:
' Like Excel's sum if function put performs an average, rather than a sum.
'
Public Function AVGIF(ByVal checkRange As Range, ByVal criteria As Variant, ByVal avgRange As Range) As Variant
Dim sumIfResult As Double
Dim countIfResult As Double
sumIfResult = Application.WorksheetFunction.SumIf(checkRange, criteria, avgRange)
countIfResult = Application.WorksheetFunction.CountIf(checkRange, criteria)
If countIfResult = 0 Then
AVGIF = 0
Else
AVGIF = sumIfResult / countIfResult
End If
End Function
I hope some one finds this as useful as I have.