0

I need help to create a VBA function that does the same thing as this excel formula =AVERAGEIF(A2:A13,"<="&E2,B2:B13)

2
  • 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. Commented 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? Commented Jul 12, 2018 at 5:02

2 Answers 2

0

The worksheetfunction.averageif method might be your answer.

For example:

WorksheetFunction.AverageIf(Range("A2:A13"),"<="&Range("E2"),Range("B2:B13"))
Sign up to request clarification or add additional context in comments.

Comments

0

Well, you can use like this,

WorksheetFunction Reference

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.