counting 'unique' instances of 'x' in a rolling range

tx12345

Board Regular
Joined
Aug 28, 2006
Messages
165
hi

this is a follow up question to a previous thread.

with this formula (thanks to PGC01):

Code:
{=MAX(COUNTIF(OFFSET($A$2:$C$11,-1+ROW(INDIRECT("1:"&ROWS($A$2:$C$11)-F4+1)),0,F4,COLUMNS($A$2:$C$11)),F3))}

we get this result using the data set shown below:

Code:
312	412	212			
912	712	312		 Target: 120
512	120	412		 'Rolling' set of rows: 4
120	120	612 <-|	       Max Result: 5
212	512	112 <-|			
512	412	912 <-|		
120	120	120 <-|	
912	512	912			
612	412	112			
312	120	812

As you can see, in the section of 4 rows highlighted, 120 indeed appears 5 times in total. But it only appears in two of the four rows, which would give another result of '2' instead of '5'.

i forgot to follow up on the other thread how to adjust the excellent formula shown above to not only show the aggregate total of all instances in each 'rolling' set, which it does very well, but also to count only the number of rows the target number appears in as well.




tx
 
Hi again tx

As you may have noticed there was one cycle missing in the second For.

I corrected it in both UDFs

Correct:

Code:
For lchar = 1 To rR.Rows.Count - lPeriod + 1

Sorry
PGC
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi tx

Here goes the Average of the running total, the last one you requested.

Cheers
PGC

Code:
Function RollingAvg(rR As Range, lTarget As Long, lPeriod As Long) As Double
Dim sCount As String, lRow As Long, lchar As Long, dAvg As Double, lTot As Long

For lRow = 1 To rR.Rows.Count
    sCount = sCount & "+" & Evaluate("=--(countif(" & rR.Rows(lRow).Address & "," & lTarget & ")>0)")
Next

For lchar = 1 To rR.Rows.Count - lPeriod + 1
    lTot = lTot + Evaluate(Mid(sCount, lchar * 2, lPeriod * 2 - 1))
Next

RollingAvg = lTot / (rR.Rows.Count - lPeriod + 1)
End Function
rollingmax.xls
ABCDEFGH
1
2312412212
3912712312Target:120
4512120412Period:5
5120120612MaxResult:3
6212512112MinResult1
7512412912Avg2
8120120120
9912512912
10612412112
11312120812
12
13
Sheet3
 
Upvote 0
thanks so much for the effective functions. i really have to learn how to do that. any resources to learn how to write functions in VBA?

thnaks

tx
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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