Rolling average

ssh99

New Member
Joined
Oct 25, 2020
Messages
30
Office Version
  1. 2010
Platform
  1. Windows
  2. MacOS
I need a rolling average in cell AA4-AA6 for data from the last 6 months covering columns C to Z.

Each month data may be added to the next column which is why I have specified a range.

There are some months where there is no data available.

The spreadsheet can be accessed on the following link: Template.xlsx

Any help would be grately appreciated! I've been searching for a solution using the OFFSET formula for hours but it is not working,
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
If you're open to using a UDF, please try the following in a standard module. I think I understand your logic - average the most recent 6 cells that are not blank?
VBA Code:
Function AVG6M() As Double
    Application.Volatile
    Dim c As Range, i As Long, j As Long, x As Long
    Set c = Application.Caller
    For i = 1 To 24
        If c.Offset(0, -i) <> "" Then
            x = x + 1
            j = j + c.Offset(0, -i).Value
            If x = 6 Then Exit For
        End If
    Next i
    AVG6M = j / x
End Function

Used like this:
Template.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1SUMMARY TABLERESULTS SUMMARY APRIL 2022 - MARCH 2023RESULTS SUMMARY APRIL 2023 - MARCH 2024
2MonthAprMayJunJulAugSepOctNovDecJanFebMarAprMayJunJulAugSepOctNovDecJanFebMarROLLING AVERAGEAVERAGEAVERAGE
3UserName2022202220222022202220222022202220222023202320232023202320232023202320232023202320232024202420246 MONTHS2022-20232023-2024
4LSE-01John Smith0.005.004.0010.0010.001.005.003.005.001.004.174.781.00
5LSE-02Tracy Hills3.004.005.005.004.000.004.003.003.003.005.003.001.005.003.333.553.00
6LSE-03Jill King0.000.005.005.004.000.003.005.0010.003.004.004.173.563.50
Sheet1
Cell Formulas
RangeFormula
AA4:AA6AA4=AVG6M()
AB4:AB6AB4=(IFERROR(AVERAGEIF(C4:N4,">=0"),"-"))
AC4:AC6AC4=(IFERROR(AVERAGEIF(O4:Z4,">=0"),"-"))
 
Upvote 0
If you're open to using a UDF, please try the following in a standard module. I think I understand your logic - average the most recent 6 cells that are not blank?
VBA Code:
Function AVG6M() As Double
    Application.Volatile
    Dim c As Range, i As Long, j As Long, x As Long
    Set c = Application.Caller
    For i = 1 To 24
        If c.Offset(0, -i) <> "" Then
            x = x + 1
            j = j + c.Offset(0, -i).Value
            If x = 6 Then Exit For
        End If
    Next i
    AVG6M = j / x
End Function

Used like this:
Template.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1SUMMARY TABLERESULTS SUMMARY APRIL 2022 - MARCH 2023RESULTS SUMMARY APRIL 2023 - MARCH 2024
2MonthAprMayJunJulAugSepOctNovDecJanFebMarAprMayJunJulAugSepOctNovDecJanFebMarROLLING AVERAGEAVERAGEAVERAGE
3UserName2022202220222022202220222022202220222023202320232023202320232023202320232023202320232024202420246 MONTHS2022-20232023-2024
4LSE-01John Smith0.005.004.0010.0010.001.005.003.005.001.004.174.781.00
5LSE-02Tracy Hills3.004.005.005.004.000.004.003.003.003.005.003.001.005.003.333.553.00
6LSE-03Jill King0.000.005.005.004.000.003.005.0010.003.004.004.173.563.50
Sheet1
Cell Formulas
RangeFormula
AA4:AA6AA4=AVG6M()
AB4:AB6AB4=(IFERROR(AVERAGEIF(C4:N4,">=0"),"-"))
AC4:AC6AC4=(IFERROR(AVERAGEIF(O4:Z4,">=0"),"-"))
Thank you for your reply. I don't recognise the VBA you've added unfortunately.

I only need the rolling average formula in column AA. The formula you've added seems quite simplified?

The formulas already in columns AB and AC are OK.
 
Upvote 0
Yes, I understand what you're looking for. To see how the VBA works (UDF = User Defined Function) press Alt+F11 - the Visual Basic Editor will appear on your screen. From the menu at the top of the screen, select Insert/Module. Copy the code from post #2 above and paste it into the window that appears on the right of the screen.

To use the UDF, simply type =AVG6M() into cell AA4. The result of the average of the last 6 cells with a value in them in row 4 should appear. Manually check the figure to see if it's what you want.

I didn't do anything with the formulas in columns AB & AC, I left them as you had provided them in your shared file.
 
Upvote 0
Yes, I understand what you're looking for. To see how the VBA works (UDF = User Defined Function) press Alt+F11 - the Visual Basic Editor will appear on your screen. From the menu at the top of the screen, select Insert/Module. Copy the code from post #2 above and paste it into the window that appears on the right of the screen.

To use the UDF, simply type =AVG6M() into cell AA4. The result of the average of the last 6 cells with a value in them in row 4 should appear. Manually check the figure to see if it's what you want.

I didn't do anything with the formulas in columns AB & AC, I left them as you had provided them in your shared file.
Thanks. I see what you've done and is calculating correctly. Is there a formula I can put directly in the cell instead? I want to avoid using VBA as then I may need to take the file macro enabled when sharing with other people.
 
Upvote 0
There may be. Formulas are not my thing, so hopefully another contributor will come to the rescue.
 
Upvote 0
Yes, I understand what you're looking for. To see how the VBA works (UDF = User Defined Function) press Alt+F11 - the Visual Basic Editor will appear on your screen. From the menu at the top of the screen, select Insert/Module. Copy the code from post #2 above and paste it into the window that appears on the right of the screen.

To use the UDF, simply type =AVG6M() into cell AA4. The result of the average of the last 6 cells with a value in them in row 4 should appear. Manually check the figure to see if it's what you want.

I didn't do anything with the formulas in columns AB & AC, I left them as you had provided them in your shared file.
Hi. I'm getting 'runtime error 424 when running the VBA, Screenshot of the line where it fails below.

1688144916289.png


Any idea what the problem is?
 
Upvote 0
Could you possibly share your file via DropBox, Google Drive or similar file sharing platform - IF the file your applying this to is different from the one you shard in post #1.
 
Upvote 0
Could you possibly share your file via DropBox, Google Drive or similar file sharing platform - IF the file your applying this to is different from the one you shard in post #1.
It's the same file I shared in post #1. Is it not opening anymore?
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,107
Members
449,096
Latest member
provoking

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top