Return to VBA Code Examples

VBA Average – AVERAGE, AVERAGEA, AVERAGEIF

This tutorial will show you how to use the Excel Average function in VBA.

The Excel AVERAGE Function is used to calculate an average from a range cells in your Worksheet that have values in them. In VBA, It is accessed using the WorksheetFunction method.

AVERAGE WorksheetFunction

The WorksheetFunction object can be used to call most of the Excel functions that are available within the Insert Function dialog box in Excel. The AVERAGE function is one of them.

vba average syntax

You are able to have up to 30 arguments in the AVERAGE  function. Each of the arguments must refer to a range of cells.

This example below will produce the average of the sum of the cells B11 to N11

The example below will produce an average of the sum of the cells in B11 to N11 and the sum of the cells in B12:N12. If you do not type the Application object, it will be assumed.

Assigning an AVERAGE result to a Variable

You may want to use the result of your formula elsewhere in code rather than writing it directly back to an Excel Range.  If this is the case, you can assign the result to a variable to use later in your code.

vba average msgbox

AVERAGE with a Range Object

You can assign a group of cells to the Range object, and then use that Range object with the WorksheetFunction object.

AVERAGE Multiple Range Objects

Similarly, you can calculate the average of the cells from multiple Range Objects.

Using AVERAGEA

The AVERAGEA function differs from the AVERAGE function in that it create an average from all the cells in a range, even if one of the cells has text in it – it replaces the text with a zero and includes that in calculating the average.   The AVERAGE function would ignore that cell and not factor it into the calculation.

In the example below, the AVERAGE function returns a different value to the AVERAGEA function when the calculation is used on cells A10 to A11

vba average averagea

The answer for the AVERAGEA formula is lower than the AVERAGE formula as it replaces the text in A11 with a zero, and therefore averages over 13 values rather than the 12 values that the AVERAGE is calculating over.

Using AVERAGEIF

The AVERAGEIF Function allows you to average the sum of a range of cells that meet a certain criteria.

The procedure above will only average the cells in range G5:G30 where the corresponding cell in column F has the word ‘Savings’ in it.  The criteria you use has to be in quotation marks.

vba average average if

 

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

Disadvantages of WorksheetFunction

When you use the WorksheetFunction to average the values in a range in your worksheet, a static value is returned, not a flexible formula. This means that when your figures in Excel change, the value that has been returned by the WorksheetFunction will not change.

vba average static

In the example above, the procedure TestAverage procedure has created the average of B11:M11 and put the answer in N11.   As you can see in the formula bar, this result is a figure and not a formula.

If any of the values change therefore in the Range(B11:M11 ), the results in N11 will NOT change.

Instead of using the WorksheetFunction.Average, you can use VBA to apply the AVERAGE Function to a cell using the Formula or FormulaR1C1 methods.

Using the Formula Method

The formula method allows you to point specifically to a range of cells eg: B11:M11 as shown below.

vba average formula

 

Using the FormulaR1C1 Method

The FomulaR1C1 method is more flexible in that it does not restrict you to a set range of cells. The example below will give us the same answer as the one above.

vba average formula r1c1

However, to make the formula more flexible, we could amend the code to look like this:

Wherever you are in your worksheet, the formula will then average the values in the 12 cells directly to the left of it and place the answer into your ActiveCell. The Range inside the AVERAGE function has to be referred to using the Row (R) and Column (C) syntax.

Both these methods enable you to use Dynamic Excel formulas within VBA.

There will now be a formula in N11 instead of a value.