How can I calculate the average value of a range in VBA? 2

How can I calculate the average value of a range in VBA?

Calculating the average value of a range in VBA involves using a formula to find the sum of all the values in the range, and then dividing that sum by the total number of values in the range. This can be done by using the “AVERAGE” function in VBA, which takes in the range of cells as its argument. The result will be the average value of the range. It is a simple and efficient way to calculate the average value of a set of data in VBA.

VBA: Calculate Average Value of Range


You can use the following basic syntax to calculate the average value of a range in Excel using VBA:

Sub AverageRange()
    Range("E2") = WorksheetFunction.Average(Range("B1:B12"))
End Sub

This particular example calculates the average value in the range B2:B12 and assigns the result to cell E2.

If you would instead like to display the average value in a message box, you can use the following syntax:

Sub AverageRange()
    'Create variable to store average valueDim avg As Single
    
    'Calculate average value of range
    avg = WorksheetFunction.Average(Range("B1:B12"))
    
    'Display the result
    MsgBox "Average Value in Range:" & avg
End Sub

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: Calculate Average of Range Using VBA and Display Results in Cell

Suppose we would like to calculate the average value in the points column and output the results in a specific cell.

We can create the following macro to do so:

Sub AverageRange()
    Range("E2") = WorksheetFunction.Average(Range("B1:B12"))
End Sub

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

Notice that cell E2 contains a value of 21.27273.

This tells us that the average value in the points column is 21.27273.

Example 2: Calculate Average of Range Using VBA and Display Results in Message Box

Suppose we would instead like to calculate the average value in the points column and output the results in a message box.

Sub AverageRange()
    'Create variable to store average valueDim avg As Single
    
    'Calculate average value of range
    avg = WorksheetFunction.Average(Range("B1:B12"))
    
    'Display the result
    MsgBox "Average Value in Range: " & avg
End Sub

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

VBA average of range

 

The message box tells us that the average value in the range B2:B12 is 21.27273.

Note that in this example we calculated the average value in the range B2:B12.

However, if you’d like to instead calculate the average value in an entire column you could type B:B instead.

This will calculate the average value for every cell in column B.

Cite this article

stats writer (2024). How can I calculate the average value of a range in VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-calculate-the-average-value-of-a-range-in-vba/

stats writer. "How can I calculate the average value of a range in VBA?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-calculate-the-average-value-of-a-range-in-vba/.

stats writer. "How can I calculate the average value of a range in VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-calculate-the-average-value-of-a-range-in-vba/.

stats writer (2024) 'How can I calculate the average value of a range in VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-calculate-the-average-value-of-a-range-in-vba/.

[1] stats writer, "How can I calculate the average value of a range in VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I calculate the average value of a range in VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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