A single, array-formula solution to this problem is possible. It requires only that a stop value "Test" be added at the bottom of your column of data.
Assuming your data are in the range A2:A18
, here is the formula that would compute the average in cell B2
:
=IF(LEFT(A1,4)="Test",
COUNTIF(OFFSET(A1,1,0,MATCH("Test*",$A2:$A$18,0)-1,1),"pass")
/ COUNTA(OFFSET(A1,1,0,MATCH("Test*",$A2:$A$18,0)-1,1)),
"")
The key parts of the formula are expressions that calculate the ranges for the two count functions, COUNTIF
and COUNTA
.
The COUNTIF function - which counts the number of passes in a group - takes two arguments, the range for the count and the condition to be met by cells counted.
I use the OFFSET
function to provide the count range. OFFSET
takes five arguments:
- An anchor cell (if a range is provided, the function only uses the top left cell in the range).
- The number of rows below (+) or above (-) the anchor cell that the range to be returned will begin.
- The number of columns to the right (+) or left (-) of the anchor cell for the start of the return range.
- The number of rows to return.
- The number of columns to return.
For example, OFFSET(A1,5,2,4,1)
will return the range C6:D9
.
In the formula, the anchor cell is A1
, the row offset is 1 and the column offset is 0, in other words, the start of the range to return is A2
.
The number of rows to return is computed by the MATCH
function. For the calculation of the average for the first test group, MATCH
looks in the range A2:A18
for the row of the first cell that starts with the word 'Test'. A2
is the cell in the pass/fail column just below the cell that starts the test group.
The last cell in the column is A18
, which has been added to the data and contains the word 'Test'. This prevents the formula from returning an error for the last test group. Otherwise, the match would return an error because it could not find another occurrence of Test
beyond the start of the final group.
Also note that the anchor points for the match range, i.e., $A2:$A$18
, leaves unanchored the row reference to the top of the range (the 2 in A2
). That's so the range will adjust downward as the formula is copied down so as to find the next, not the previous, occurrence of 'Test'.
Finally, I decrease the number of return rows by 1 to exclude the Text
that MATCH
found, which belongs to the next group.
The COUNTA
functions - which counts the total number of passes and fails in a group - uses the same OFFSET/MATCH expression to get the range for the group.
This is an array formula and so must be entered with the Control-Shift-Enter key combination. Copy the formula down to the bottom of the data (excluding the cell with the 'Test' stop value) to calculate the averages for each group.
For convenience, here is the formula without the breaks shown above. It can be directly pasted into the worksheet (remembering to confirm it with Control-Shift-Enter).
=IF(LEFT(A1,4)="Test",COUNTIF(OFFSET(A1,1,0,MATCH("Test*",$A2:$A$18,0)-1,1),"pass")/COUNTA(OFFSET(A1,1,0,MATCH("Test*",$A2:$A$18,0)-1,1)),"")