Average last n values in a column

To average the last n values in a column you can use a combination of Excel AVERAGE, OFFSET and COUNT functions, using Excel and VBA

Example: Average last n values in a column

Average last n values in a column

METHOD 1. Average last n values in a column

EXCEL

=AVERAGE(OFFSET(B5,COUNT(B5:B9)-1,0,-D5))
This formula averages the last n values from a specific column, using a combination of the Excel AVERAGE, OFFSET and COUNT functions. In this example the formula averages the last three values (the number is captured in cell D5 and referenced in the formula) in column B.
Please note that if there is a blank cell or a non numeric value in the column the formula will take this cell into consideration and will apply a value of 0 against the cell.

METHOD 1. Average last n values in a column using VBA

VBA

Sub Average_last_n_values_in_a_column()
'declare a variable
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'sum the last three values in a column
ws.Range("E3") = Application.Average(Range("B5").Offset(Application.Count(Range("B5:B9")) - ws.Range("D5"), 0).Resize(Range("D5")))

End Sub

PREREQUISITES
Worksheet Name: Have a worksheet named Analysis.
First cell in range: If using the exact same VBA code, the first cell in a range needs to be ("B5").
Last n values: In this example we are referencing to a cell ("D5") that contains a number of the last n values to average. Therefore, if using the exact same VBA code you will need to capture a number of the last n values to average in cell ("D5").
Column Range: In this example the values that we are averaging are captured in range ("B5:B9"). Therefore, if using the exact same VBA code you will need to capture the values that you want to average in range ("B5:B9").
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell references ("E5") in the VBA code to any cell in the worksheet, that doesn't conflict with the formula.
First cell in range: Select the first cell in a range (column) from which to average the last n values by changing the cell reference ("B5") in the VBA code to any cell that doesn't conflict with the formula.
Last n values: Select the last number of values that you want to average in the specified column by changing the cell reference ("D5") in the VBA code to any cell that contains the number and doesn't conflict with the formula. You can also enter the number directly in the VBA code, by replacing 'Range("D5")' with a number.
Column Range: Select the column range where the values are captured that you want to average by changing the range reference ("B5:B9").

Explanation about how to average last n values in a column

EXPLANATION

EXPLANATION
This tutorial shows and explains how to average last n values in a column, by using an Excel formula or VBA.

Excel Method: This tutorial provides a single Excel method that can be applied to average last n values in a column, using a combination of Excel AVERAGE, OFFSET and COUNT functions.

VBA Methods: This tutorial provides a single VBA method that averages the last n values in a column by applying the AVERAGE, OFFSET and COUNT functions in the VBA code.

FORMULA
AVERAGE(OFFSET(first_cell,COUNT(range)-1,0,-last_n_values))
ARGUMENTS
first_cell: The first cell in a specific column from which to begin averaging the last n values.
last_n_values: A number that represents the last n values to be averaged.
range: A range of cells (in a single column) that captures the values from which you want to average the last n values.
ADDITIONAL NOTES
Note 1: If a blank cell or a non-numeric value is captured in the last n values to be averaged, the formula treats these the same as 0.

RELATED TOPICS

Related Topic Description Related Topic and Description
How to average the first n values in a row using Excel and VBA methods
How to average the first n values in a column using Excel and VBA methods
How to sum cells with values in odd columns using Excel and VBA methods
How to sum the first n values in a column using Excel and VBA methods
How to sum the first n values in a row using Excel and VBA methods

RELATED FUNCTIONS

Related Functions Description Related Functions and Description
The Excel AVERAGE function returns the average value from a specified range
The Excel COUNT function returns the number of cells that contain numeric values in a specified range