30 Advanced Excel Tips for 2026

Excel Champs / VBA / How to Average Values in Excel using VBA
By Puneet Gogia (Microsoft MVP)

How to Average Values in Excel using VBA

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Range("B1").Value = WorksheetFunction.Average(Range("A1:A10")) writes the average of A1:A10 to B1 as a fixed value. Use Range("B1").Formula = "=AVERAGE(A1:A10)" instead if the result should update when the numbers change.

Store the result in a Double. A Long or Integer variable rounds it to a whole number, so an average of 7.6 shows as 8.

Blank cells and text are left out of the average, but zeros count and pull it down. To skip zeros, use WorksheetFunction.AverageIf(Range("A1:A10"), "<>0"). For a condition on another column, use WorksheetFunction.AverageIfs(Range("C2:C100"), Range("A2:A100"), "East").

If the range holds no numbers, AVERAGE returns #DIV/0!, and WorksheetFunction.Average stops the macro with run-time error 1004. An error value such as #N/A in the range does the same. Check first with If WorksheetFunction.Count(Range("A1:A10")) > 0 Then.

In Excel, you can use VBA to calculate the average values from a range of cells or multiple ranges. In this tutorial, we are going to learn the different ways to do it.

Average in VBA using WorksheetFunction

In VBA, there are multiple functions that you can use, but there’s no specific function for this purpose. That does not mean we can’t calculate an average. In VBA, there’s a property called WorksheetFunction that helps you call worksheet functions in a VBA code.

average-in-vba-using-worksheet-function

Let’s average values from the range A1:A10.

  1. First, enter the WorksheetFunction property and then select the AVERAGE function from the list.
    worksheet-function-average
  2. Next, you need to enter the starting parenthesis, as you do while entering a function in the worksheet.
    enter-starting-paranthesis
  3. After that, we need to use the range object to refer to the range for which we want to calculate the average.
    use-the-range-object
  4. In the end, type the closing parenthesis and assign the function’s return value to cell B1.
    type-close-pranthesis
Range("B1") = Application.WorksheetFunction.Average(Range("A1:A10"))

Now when you run this code, it will calculate the average for the values that you have in the range A1:A10 and enter the value in cell B1.

run-the-code-calculates-average

Average Values from an Entire Column or a Row

If you want to average the values from an entire column or row, you just need to specify a row or column instead of the range that we have used in the earlier example. Make sure the result cell is not in the column or row that you average. That’s why the row example enters the result in cell A3.

'for the entire column A
Range("B1") = Application.WorksheetFunction.Average(Range("A:A"))

'for entire row 1
Range("A3") = Application.WorksheetFunction.Average(Range("1:1"))

Use VBA to Average Values from the Selection

Now, let’s say you want to average the values from the selected cells only. In that case, you can use a code just like the following.

Sub vba_average_selection()

Dim sRange As Range
Dim iAverage As Double

On Error GoTo errorHandler

Set sRange = Selection

iAverage = WorksheetFunction.Average(Range(sRange.Address))
MsgBox iAverage

Exit Sub

errorHandler:
MsgBox "make sure to select a valid range of cells"

End Sub

In the above code, we have assigned the selection to the variable “sRange” and then used that range variable’s address to get the average. The variable for the result is a Double, so the average keeps its decimals.

VBA Average All Cells Above

The following code takes all the cells above the selected cell, averages the values from them and enters the result in the selected cell.

Sub vba_auto_Average()

Dim iFirst As String
Dim iLast As String
Dim iRange As Range

On Error GoTo errorHandler

iFirst = Selection.End(xlUp).End(xlUp).Address
iLast = Selection.End(xlUp).Address

Set iRange = Range(iFirst & ":" & iLast)
ActiveCell = WorksheetFunction.Average(iRange)

Exit Sub

errorHandler:
MsgBox "make sure to select a valid range of cells"

End Sub

Average a Dynamic Range using VBA

In the same way, you can use a dynamic range while using VBA to average values. The code below averages a 5 x 5 range that starts one row below and one column to the right of the selected cell, so the range moves with your selection.

Sub vba_dynamic_range_average()

Dim iFirst As String
Dim iLast As String
Dim iRange As Range

On Error GoTo errorHandler

iFirst = Selection.Offset(1, 1).Address
iLast = Selection.Offset(5, 5).Address

Set iRange = Range(iFirst & ":" & iLast)
ActiveCell = WorksheetFunction.Average(iRange)

Exit Sub

errorHandler:
MsgBox "make sure to select a valid range of cells"

End Sub

Average a Dynamic Column or a Row

In the same way, if you want to use a dynamic column, you can use the following code, which takes the column of the active cell and averages all the values that you have in it.

Sub vba_dynamic_column()

Dim iCol As Long

On Error GoTo errorHandler

iCol = ActiveCell.Column
MsgBox WorksheetFunction.Average(Columns(iCol))

Exit Sub

errorHandler:
MsgBox "make sure to select a valid range of cells"

End Sub

And here’s the code for a row.

Sub vba_dynamic_row()

Dim iRow As Long

On Error GoTo errorHandler

iRow = ActiveCell.Row

MsgBox WorksheetFunction.Average(Rows(iRow))

Exit Sub

errorHandler:
MsgBox "make sure to select a valid range of cells"

End Sub

Here, the code uses the active cell’s row, so it averages all the values in that row.

Average Values from Multiple Ranges

Just like the AVERAGE function in a worksheet, you can give more than one range to WorksheetFunction.Average, separated by commas. The code below averages all the values from A1:A10 and C1:C10 together:

Sub vba_average_multiple_ranges()
Range("E1") = Application.WorksheetFunction.Average(Range("A1:A10"), Range("C1:C10"))
End Sub

This gives you the average of all the numbers in both ranges, not the average of two separate averages.

Average with a Condition (AVERAGEIF and AVERAGEIFS)

To average only the values that meet a condition, use AverageIf. In the code below, it averages the values in column A where column B has “North”:

Sub vba_averageif()
Range("E1") = Application.WorksheetFunction.AverageIf(Range("B1:B10"), "North", Range("A1:A10"))
End Sub

For more than one condition, use AverageIfs. Here, the range to average comes first, and then the pairs of range and criteria. The code below averages the values in column A where column B has “North” and the value is greater than 0:

Sub vba_averageifs()
Range("E1") = Application.WorksheetFunction.AverageIfs(Range("A1:A10"), Range("B1:B10"), "North", Range("A1:A10"), ">0")
End Sub

Average Values but Ignore Zeros

AVERAGE skips blank cells and text, but it counts zeros. If you don’t want the zeros in the average, use AverageIf with the “<>0” criteria:

Sub vba_average_ignore_zeros()
Range("B1") = Application.WorksheetFunction.AverageIf(Range("A1:A10"), "<>0")
End Sub

Handle the Error When There Are No Numbers

In a worksheet, AVERAGE returns #DIV/0! when the range has no numbers. In VBA, WorksheetFunction.Average stops the code with a run-time error instead. The same happens if any cell in the range has an error value, like #N/A. That’s why the codes above use an error handler.

Another way is to count the numbers first, and only calculate the average if there’s at least one:

Sub vba_average_check()
Dim rng As Range
Set rng = Range("A1:A10")
If WorksheetFunction.Count(rng) = 0 Then
MsgBox "There are no numbers in the range."
Else
Range("B1") = WorksheetFunction.Average(rng)
End If
End Sub

Enter the AVERAGE Formula with VBA

All the codes above put a fixed value in the cell, so the result doesn’t change when the values change. If you want the result to update on its own, use the code to enter the formula in the cell:

Sub vba_average_formula()
Range("B1").Formula = "=AVERAGE(A1:A10)"
End Sub

Frequently Asked Questions

Why does my VBA average show a whole number?

The variable that stores the result is probably declared as Long or Integer, which can’t hold decimals, so VBA rounds the average. Declare it as Double instead.

Does VBA average include blank cells and text?

No. When you give a range, it skips blank cells, text (including numbers stored as text) and TRUE/FALSE values. Cells with 0 are included.

How do I average values on another sheet?

Put the sheet name in front of the range, like WorksheetFunction.Average(Worksheets(“Sheet2”).Range(“A1:A10”)). You don’t need to activate that sheet first.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel