I need help to create a VBA function that does the same thing as this excel formula =AVERAGEIF(A2:A13,"<="&E2,B2:B13)
-
Welcome to Stack Overflow, glad to see a new member! Please check How to Ask and make your question a Minimal, Complete, and Verifiable example.ForeverZer0– ForeverZer02018-07-12 04:39:20 +00:00Commented Jul 12, 2018 at 4:39
-
What have you tried? This shouldn't be too difficult to have a first stab at. Are the ranges always going to be fixed though? Or are they arguments you pass to your user defined function?QHarr– QHarr2018-07-12 05:02:40 +00:00Commented Jul 12, 2018 at 5:02
Add a comment
|
2 Answers
The worksheetfunction.averageif method might be your answer.
For example:
WorksheetFunction.AverageIf(Range("A2:A13"),"<="&Range("E2"),Range("B2:B13"))
Comments
Well, you can use like this,
Sub average()
MsgBox (WorksheetFunction.AverageIf(Worksheets(1).Range("A2:A13"), "<=" & Worksheets(1).Range("E1").Value, Worksheets(1).Range("B2:B13")))
'or like this to put in the range.
Worksheets(1).Range("F1").Formula = "=AVERAGEIF(A2:A13," >= "&E1,B2:B13)"
End Sub