How do I write the AVERAGEIF and AVERAGEIFS functions in VBA?” 2

How do I write the AVERAGEIF and AVERAGEIFS functions in VBA?”

The AVERAGEIF and AVERAGEIFS functions in VBA allow users to calculate the average of a range of values based on specific criteria. These functions are useful for analyzing large data sets and can be easily implemented in VBA code. To use the AVERAGEIF function, users must specify the range of values and the criteria to be met. For the AVERAGEIFS function, multiple criteria can be specified to further refine the average calculation. By using these functions, users can efficiently analyze data and make informed decisions.

VBA: Write AVERAGEIF and AVERAGEIFS Functions


You can use the following methods to write AVERAGEIF and AVERAGEIFS functions using VBA in Excel:

Method 1: AVERAGEIF Function in VBA

Sub Averageif_Function()
    Range("E2") = WorksheetFunction.AverageIf(Range("A2:A12"), "Mavs", Range("B2:B12"))
End Sub

This particular example will calculate the average value in the range B2:B12 where the corresponding value in the range A2:A12 is equal to “Mavs” and then assign the result to cell E2.

Method 2: AVERAGEIFS Function in VBA

Sub Averageifs_Function()
    Range("E2") = WorksheetFunction.AverageIfs(Range("C2:C12"), Range("A2:A12"), "Mavs", Range("B2:B12"), ">20")
End Sub

This particular example will calculate the average value in the range C2:C12 where the value in the range A2:A12 is equal to “Mavs” and the value in the range B2:B12 is greater than 20 and then assign the result to cell E2.

The following examples shows how to use each of these methods in practice with the following dataset in Excel that contains information about various basketball players:

Example 1: AVERAGEIF Function in VBA

Suppose we would like to calculate the average value in the points column for players who are on the Mavs team.

We can create the following macro to perform this AVERAGEIF function:

Sub Averageif_Function()
    Range("E2") = WorksheetFunction.AverageIf(Range("A2:A12"), "Mavs", Range("B2:B12"))
End Sub

When we run this macro, we receive the following output:

Notice that cell E2 contains a value of 18.25.

This tells us that the average value in the points column for the players on the Mavs team is 18.25.

Average points for Mavs players: (22 + 10 + 29 + 12) / 4 = 18.25.

Example 2: AVERAGEIFS Function in VBA

Suppose we would like to calculate the average value in the assists column for the players that meet the following criteria:

  • Player is on the Mavs team.
  • Player scored more than 20 points.

We can create the following macro to perform this AVERAGEIFS function:

Sub Averageifs_Function()
    Range("E2") = WorksheetFunction.AverageIfs(Range("C2:C12"), Range("A2:A12"), "Mavs", Range("B2:B12"), ">20")
End Sub

When we run this macro, we receive the following output:

Notice that cell E2 contains a value of 9.

This tells us that the average assists among players who are on the Mavs team and scored more than 20 points is 9.

Note: In this example, we created a AVERAGEIFS function using two criteria ranges but you can use as many criteria ranges as you’d like within the WorksheetFunction.AverageIfs method.

Cite this article

stats writer (2024). How do I write the AVERAGEIF and AVERAGEIFS functions in VBA?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-write-the-averageif-and-averageifs-functions-in-vba/

stats writer. "How do I write the AVERAGEIF and AVERAGEIFS functions in VBA?”." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-write-the-averageif-and-averageifs-functions-in-vba/.

stats writer. "How do I write the AVERAGEIF and AVERAGEIFS functions in VBA?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-write-the-averageif-and-averageifs-functions-in-vba/.

stats writer (2024) 'How do I write the AVERAGEIF and AVERAGEIFS functions in VBA?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-write-the-averageif-and-averageifs-functions-in-vba/.

[1] stats writer, "How do I write the AVERAGEIF and AVERAGEIFS functions in VBA?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do I write the AVERAGEIF and AVERAGEIFS functions in VBA?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top