Create an Excel Macro to Average a Variable Number of Rows
Question details
Create a reusable Excel macro that averages a data range without hard-coding its last row.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The number of data rows changes, and the user wants an average that includes the current data.
- Observed behavior
- The desired macro should identify the current last row and average the values in the data column.
Identify the data column, its header row, and an empty cell for the result. The example uses column C with a header in C1 and writes the average to E2.
Use VBA to Find the Last Row and Calculate the Average
Use this macro when you want to recalculate the average on demand as rows are added or removed.
The macro finds the last used row in column C and averages C2 through that row on the active worksheet. It ignores blank cells and warns you if the range contains no numbers.
In desktop Excel, open the Developer tab and select Visual Basic. If Developer is hidden, enable it in Excel's ribbon settings.
In the VBA editor, select Insert > Module and paste: Sub AverageVariableRows() Dim ws As Worksheet Dim lastRow As Long Dim dataRange As Range Set ws = ActiveSheet lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row If lastRow < 2 Then MsgBox "No data found in column C." Exit Sub End If Set dataRange = ws.Range("C2:C" & lastRow) If Application.WorksheetFunction.Count(dataRange) = 0 Then MsgBox "No numeric values found in column C." Exit Sub End If ws.Range("E2").Value = Application.WorksheetFunction.Average(dataRange) End Sub
Select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm) so the macro is retained.
Select the worksheet containing the data, then choose Developer > Macros, select AverageVariableRows, and click Run. The result appears in E2.

Average an Excel Table Column Without a Macro
Use a Table formula if you need the average to update as the data grows but do not need VBA.
Average a Changing Data Range in WPS Spreadsheet
In a WPS Spreadsheet edition with VBA support, you can use a macro to find the last row and calculate the average. VBA availability and macro support vary by edition and platform.
- 1. Open the workbook: In WPS Spreadsheet, select File > Open and choose your workbook. Place numeric data in column C starting at C2.
- 2. Check VBA support: Look for the Developer tab and its Visual Basic or Macros commands. If they are unavailable, check whether your WPS edition and platform support VBA.
- 3. Add the macro: In a VBA-enabled edition, open Developer > Visual Basic, select Insert > Module, and paste the AverageVariableRows macro shown above.
- 4. Save and run: Use File > Save As to save as an XLSM workbook, then select the data worksheet and run AverageVariableRows from Developer > Macros. Check E2 for the result.

Frequently Asked Questions
Does the macro include blank cells in the average?
The AVERAGE function ignores blank cells within C2 through the last used row. Numeric zeros are included.
What happens if column C has no numbers?
The example checks the range with COUNT. If it finds no numeric values, it displays a message and leaves the existing value in E2 unchanged.
How do I use a different data column?
In the VBA editor, change both references to column C in the lastRow and dataRange lines. Also change C2 if your data starts on a different row.
Can I run the VBA macro in Excel for the web?
Excel for the web cannot create or run VBA macros. Open the workbook in desktop Excel and use Developer > Macros to run it.




