Averaging last 3 numbers in a column that match criteria from another column??

apwaterski

New Member
Joined
Feb 28, 2016
Messages
5
How do I average the last 3 numbers in a column, that match a word in another column. For example...
Column A1:A10 = green, green, red, red, green, blue, green, green, red, green.
Column B1:B10= 1,9,7,3,8,7,6,3,1,9.
I want to average the last 3 numbers in column B, that are green. The answer is 6 (the average of 9,3,6). I will be adding to both column A and B daily, and I want the formula to always find the average of the LAST 3 numbers in column B that are green.
Thanks for the help!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I'm guessing there will be a formula solution, but in the meantime will a VBA do !!
This will put the answer in Cell C1
and will work regardless of the length o Columns A & B
Code:
Sub MM1()
Dim lr As Long, r As Long, n As Integer
lr = Cells(Rows.Count, "A").End(xlUp).Row
n = 0
x = 0
For r = lr To 1 Step -1
    If Range("A" & r).Value = "green" Then
        n = n + Range("B" & r).Value
        x = x + 1
    If x = 3 Then Range("C1").Value = n / 3
    End If
Next r
End Sub
 
Upvote 0
Let's define color in Formulas | Name Manager as referring to:

=Sheet1!$A$1:INDEX(Sheet1!$A:$A,MATCH(REPT("z",255),Sheet1!$A:$A))

and value as:

=Sheet1!$B$1:INDEX(Sheet1!$B:$B,MATCH(REPT("z",255),Sheet1!$A:$A))

Note. Adjust the sheet name to suit.

Row\Col
A​
B​
C​
D​
1​
green
1​
green
2​
green
9​
6​
3​
red
7​
4​
red
3​
5​
green
8​
6​
blue
7​
7​
green
6​
8​
green
3​
9​
red
1​
10​
green
9​

In D2 control+shift+enter, not just enter:

=AVERAGE(IF(ROW(color)>=LARGE(IF(color=D1,ROW(color)),MIN(3,COUNTIFS(color,D1))),IF(color=D1,value)))
 
Upvote 0
Thanks for the help! That's exactly what I'm looking for. The formula does not work on my tablet. Do you know of any way to make an array formula work on excel mobile?
 
Upvote 0
Thanks for the help! That's exactly what I'm looking for.

You are welcome.

The formula does not work on my tablet. Do you know of any way to make an array formula work on excel mobile?

I'm not familiar with "excel mobile". Would the following work?
Rich (BB code):
=SUMPRODUCT(--(ROW(color)>=LARGE(INDEX((color=D1)*ROW(color),0),
    MIN(3,COUNTIFS(color,D1)))),(color=D1)*value)/MIN(3,COUNTIFS(color,D1))
 
Upvote 0
I'm not familiar with "excel mobile". Would the following work?
Rich (BB code):
=SUMPRODUCT(--(ROW(color)>=LARGE(INDEX((color=D1)*ROW(color),0),
    MIN(3,COUNTIFS(color,D1)))),(color=D1)*value)/MIN(3,COUNTIFS(color,D1))
[/QUOTE]

That's incredible! This formula works on excel mobile. Thanks so much!
 
Upvote 0
I'm not familiar with "excel mobile". Would the following work?
Rich (BB code):
=SUMPRODUCT(--(ROW(color)>=LARGE(INDEX((color=D1)*ROW(color),0),
    MIN(3,COUNTIFS(color,D1)))),(color=D1)*value)/MIN(3,COUNTIFS(color,D1))

That's incredible! This formula works on excel mobile. Thanks so much!

This is great. Thanks for the update.
 
Upvote 0

Forum statistics

Threads
1,216,000
Messages
6,128,204
Members
449,435
Latest member
Jahmia0616

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