Calculate a Rolling Average of the 32 Largest Values in Excel VBA
Question details
Create a VBA macro that processes rolling 96-row ranges, calculates the average of the 32 largest values in each range, and writes the results to an output column.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- The user needs to analyze values in column C by moving a 96-row window down one row at a time and outputting the calculated average in column M.
- Observed behavior
- The required result is the average of the 32 highest numeric values from each 96-row window, with the first output written to cell M96.
Confirm that column C contains at least 32 numeric values in every 96-row window. Replace "Sheet1" in the macro with the exact name of the worksheet that contains your source data.
Use WorksheetFunction.Large in a rolling VBA loop
Use this method when you want one result for every 96-row window, shifted down by one row each time.
The macro evaluates C2:C97 first and writes the result to M96. Each following loop shifts the source range down one row, averages the 32 largest values, and writes the next result one row lower in column M.
In Excel, press Alt+F11, then select Insert > Module to create a standard VBA module.
Paste the following code into the module: Sub CalculateRollingAverage() Dim ws As Worksheet Dim i As Long, j As Long Dim sourceRange As Range Dim total As Double Set ws = ThisWorkbook.Worksheets("Sheet1") For i = 2 To 906 Set sourceRange = ws.Range(ws.Cells(i, 3), ws.Cells(i + 95, 3)) total = 0 For j = 1 To 32 total = total + WorksheetFunction.Large(sourceRange, j) Next j ws.Cells(i + 94, 13).Value = total / 32 Next i End Sub
Place the cursor inside CalculateRollingAverage and press F5, or select Developer > Macros, choose CalculateRollingAverage, and click Run.
Review column M. The first result appears in M96 and, with the loop ending at i = 906, the final result appears in M1000.

Run a rolling top-32 average macro in WPS Spreadsheets
WPS Spreadsheets provides a familiar worksheet layout for reviewing rolling calculations and supports VBA macro workflows in supported desktop editions. Open the workbook, add the macro in the VBA editor, and run it to calculate the average of the 32 largest values for each 96-row range.
- 1. Open the workbook in WPS Spreadsheets: Launch WPS Office, select Spreadsheets, and open the Excel workbook containing the values in column C.
- 2. Enable macro access if available: Open the Developer or Macro tools in WPS Spreadsheets and enable macros according to your organization’s security settings.
- 3. Add the VBA procedure: Open the Visual Basic Editor, choose Insert > Module, and paste the rolling-average VBA procedure into the new module.
- 4. Update the worksheet name: Change ThisWorkbook.Worksheets("Sheet1") to the worksheet name used in your WPS workbook.
- 5. Run and review the results: Run the macro and verify that results begin in M96 and continue down column M for each rolling source range.

Frequently Asked Questions
Why does the first rolling-average result appear in M96?
The first source window starts at C2 and ends at C97, which contains 96 rows. The macro writes that window's result to row 96, so the first output cell is M96.
How do I make the macro write the final result to M1001?
Change the loop line from For i = 2 To 906 to For i = 2 To 907. Make sure column C contains enough rows for the final window, which ends at row i + 95.
What happens if a 96-row range contains fewer than 32 numbers?
WorksheetFunction.Large returns an error when it cannot find the requested rank. Ensure every rolling range has at least 32 numeric values, or add error handling to skip incomplete ranges.
Can I change the macro to average the 10 or 50 largest values instead?
Yes. Change both the inner loop limit, For j = 1 To 32, and the divisor, total / 32, to the number of largest values you want to average.
Which columns does the VBA code use?
The code reads source values from column C, where Cells(row, 3) refers to column C, and writes results to column M, where Cells(row, 13) refers to column M.




