Function to get moving average of last 5 rows that contain "x"

Reboshua

Board Regular
Joined
Jan 23, 2015
Messages
88
I have a spreadsheet that has data for player performances in NBA fantasy sports. A sample of that is shown below, but the actual file has thousands of rows and contains data for all of the players, not just Aaron Gordon below.

I'd like to learn how to create a custom function to be used in columns D and E that will calculate an "x" day moving average for the data in column C where X is whatever I define it to be in the function. In this case, it would be 5 days for column D and 2 for column E, but I'd like the ability to easily manipulate that.

I was watching a video where the gentleman had the following code, but when I ran it on my sheet, I got an "end if without a block if" error and I didn't understand how to debug it. I don't know VB. Here is his code.

Thanks in advance!

~~~~~~~~~~~~~~~~

Public Function MAVG(PlayerName, Games, RowStart) As Single

'Use this only on Worksheet PlayerbyGame

Dim Game_Cnt As Integer
Dim N As Long

Dim FD As Single
Dim Sigma_Score
'Dim Game_Cnt as Interger
Dim Temp_Name As String

For N = RowStart To 4 Step -1
Temp_Name = Worksheets("PlayerbyGame").Cells(N, 1)

If InStr(Temp_Name, PlayerName) > 0 Then
Sigma_Score = Sigma_Score + Worksheets("PlayerbyGame").Cells(N, 31)
Game_Cnt = Game_Cnt + 1


End Function
~~~~~~~~~~~~~~~~~~~~~~~~

Excel 2016 (Windows) 64 bit
[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td]Date2[/td][td]PLAYER FULL NAME[/td][td]FD[/td][td]5DMA[/td][td]3DMA[/td][/tr]

[tr=bgcolor:#FFFFFF][td]
10/17/2018​
[/td][td]Aaron Gordon[/td][td]
44.2​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
10/19/2018​
[/td][td]Aaron Gordon[/td][td]
29​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
10/20/2018​
[/td][td]Aaron Gordon[/td][td]
46.9​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
10/22/2018​
[/td][td]Aaron Gordon[/td][td]
19.1​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
10/25/2018​
[/td][td]Aaron Gordon[/td][td]
25.8​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
10/27/2018​
[/td][td]Aaron Gordon[/td][td]
20​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
10/30/2018​
[/td][td]Aaron Gordon[/td][td]
44.5​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
11/2/2018​
[/td][td]Aaron Gordon[/td][td]
9.6​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
11/4/2018​
[/td][td]Aaron Gordon[/td][td]
37.6​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
11/5/2018​
[/td][td]Aaron Gordon[/td][td]
37.3​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
11/7/2018​
[/td][td]Aaron Gordon[/td][td]
44​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
11/9/2018​
[/td][td]Aaron Gordon[/td][td]
34.4​
[/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td]
11/12/2018​
[/td][td]Aaron Gordon[/td][td]
44.5​
[/td][td][/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: PlayerbyGame[/td][/tr][/table]
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
The row that begins with If instr( and ends with Then is the problem. Try putting all the three rows beneath on the same row(Line) as the if statement.
 
Upvote 0
You are missing the End If from here
Code:
If InStr(Temp_Name, PlayerName) > 0 Then
    Sigma_Score = Sigma_Score + Worksheets("PlayerbyGame").Cells(N, 31)
    Game_Cnt = Game_Cnt + 1
[COLOR=#0000ff] End If[/COLOR]

End Function
 
Upvote 0
Thanks for the replies so far. I correctly indented the code in the workbook. I also added the End If statement.

I am now getting a "for without next" error and don't know what to do about that.

The more important problem for me is indicating the row number to start from. I need each row to calculate the 5 previous values in the sheet for Aaron Gordon, but I don't know how in the function to indicate "current row."

Any advice?

Thank you.
 
Upvote 0
As the code you posted is incomplete & doesn't do anything, can you please explain exactly what you want & show examples of the expected result
 
Upvote 0
Fluff, thank you for the fast reply. I tried to articulate what I wanted in my original post above. I was hoping his code would be an easy bridge, but perhaps not.

I'd like a formula/function in D2 (and similarly E2) that will calculate the moving average of values in column C for the number of days I indicate in the function, but will only consider those rows above the current row and only calculate the values for the players who match the text string in column B.

To be specific, I want the value in D7 to equal 33 and the value in D8 to equal 28.16.

TIA,

David
 
Last edited:
Upvote 0
Ok, how about
Code:
Function Reboshua(Rng As Range, Dys As Long) As Double
   Dim i As Long, Rw As Long, Cnt As Long
   Dim Plyr As String

   Rw = Application.Caller.Cells.Row - 1
   Plyr = Rng.Cells(Rw, 1)
   For i = Rw - 1 To 1 Step -1
      If Rng(i, 1) = Plyr Then
         Cnt = Cnt + 1
         Reboshua = Reboshua + Rng(i, 2).Value
      End If
      If Cnt = Dys Then
         Reboshua = Reboshua / Cnt
         Exit Function
      End If
   Next i
   If Cnt < Dys Then Reboshua = 0
End Function
Used like


Excel 2013/2016
ABCDE
1Date2PLAYER FULL NAMEFD5DMA3DMA
210/17/2018Aaron Gordon44.200
310/19/2018Aaron Gordon2900
410/20/2018Aaron Gordon46.9036.6
510/22/2018Aaron Gordon19.1037.95
610/25/2018Aaron Gordon25.8033
710/27/2018Aaron Gordon203322.45
810/30/2018Aaron Gordon44.528.1622.9
911/02/2018Aaron Gordon9.631.2632.25
1011/04/2018Aaron Gordon37.623.827.05
1111/05/2018Aaron Gordon37.327.523.6
1211/07/2018Aaron Gordon4429.837.45
1311/09/2018Aaron Gordon34.434.640.65
1411/12/2018Aaron Gordon44.532.5839.2
All
Cell Formulas
RangeFormula
D2=Reboshua($B$2:$B$14,5)
E2=Reboshua($B$2:$B$14,2)
 
Upvote 0
Thanks for trying, but that didn't work for me. I don't see in the code any reference to the values in column C, which is what I need to average. When I run this code, I get a 0 in d2 and #Value ! errors in each successive cell in the column below D2.

Thoughts?
 
Upvote 0
Does your data look the same as my test data in post#7?
 
Upvote 0
Not exactly, no. There are other columns and there likely will be some additional ones added over time, so I need the function to allow me to indicate where the data is, if that makes sense.

Really appreciate you helping me here.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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