VBA & Macro Problems

Create an Excel Macro to Average a Variable Number of Rows

Muhammad TalhaMuhammad Talha Nov 18, 2026 869 views

Question details

Create a reusable Excel macro that averages a data range without hard-coding its last row.

Create an Excel Macro to Average a Variable Number of Rows
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In desktop Excel, open the Developer tab and select Visual Basic. If Developer is hidden, enable it in Excel's ribbon settings.

2
Insert the macro

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

3
Save as a macro-enabled workbook

Select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm) so the macro is retained.

4
Run the macro

Select the worksheet containing the data, then choose Developer > Macros, select AverageVariableRows, and click Run. The result appears in E2.

Use VBA to Find the Last Row and Calculate the Average
Adjust the example: Change column C, the starting row, or E2 in the code to match your worksheet. E2 is overwritten when the macro succeeds; if no numeric data is found, its existing value remains.
Work with changing spreadsheet data

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. 1. Open the workbook: In WPS Spreadsheet, select File > Open and choose your workbook. Place numeric data in column C starting at C2.
  2. 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. 3. Add the macro: In a VBA-enabled edition, open Developer > Visual Basic, select Insert > Module, and paste the AverageVariableRows macro shown above.
  4. 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.
Recalculate an average for a changing number of rowsOpen and work with Microsoft Excel XLSX filesUse XLSM files for macros in a compatible, VBA-enabled editionWork in a familiar spreadsheet interface
microsoft office alternative - wps office

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.