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
Date2PLAYER FULL NAMEFD5DMA3DMA
10/17/2018​
Aaron Gordon
44.2​
10/19/2018​
Aaron Gordon
29​
10/20/2018​
Aaron Gordon
46.9​
10/22/2018​
Aaron Gordon
19.1​
10/25/2018​
Aaron Gordon
25.8​
10/27/2018​
Aaron Gordon
20​
10/30/2018​
Aaron Gordon
44.5​
11/2/2018​
Aaron Gordon
9.6​
11/4/2018​
Aaron Gordon
37.6​
11/5/2018​
Aaron Gordon
37.3​
11/7/2018​
Aaron Gordon
44​
11/9/2018​
Aaron Gordon
34.4​
11/12/2018​
Aaron Gordon
44.5​
Sheet: PlayerbyGame
 
Last edited:
It always helps if you give us exact details, rather than trying to simplify things.

Try
Code:
Function Reboshua(Rng1 As Range, Rng2 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 = Rng1.Cells(Rw, 1)
   For i = Rw - 1 To 1 Step -1
      If Rng1(i, 1) = Plyr Then
         Cnt = Cnt + 1
         Reboshua = Reboshua + Rng2(i, 1).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
=Reboshua($B$2:$B$14,$C$2:$C$14,5)
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Fluff - thanks again for your help. I think this worked, but several times excel hung for a long time and when I hit escape I got a debug prompt. My sheet has 22000 rows or so - so I don't know if there's an actual bug or if it's just taking a long time to cycle through and is being interrupted when I hit Escape.

Thoughts?
 
Upvote 0
With 22,000 rows it will take quite some time.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,037
Members
449,205
Latest member
Eggy66

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