Calculate Number of Consecutive Groups of Cells from the End

LabMadeMonk

New Member
Joined
Dec 23, 2018
Messages
2
Hello!

I want to calculate the following for each row:
How many connected cells at the end of the row contain a character such as 'x', where cells separated by 'interval-1' empty cells are still considered in one chain and are counted. A chain of 'x' cells is only broken by 'interval' amount of consecutive empty cells.

The result should show the number of 'x' cells inside of that last chain, even if the distance between them is less than 'interval' (further apart isn't allowed, but closer together is allowed - i.e. it should count the number of values in the chain, not how many times 'interval' fits into the chain.)

My table looks something like the table below.
I highlighted the chains that are considered, and marked where chains break using a hyphen (-).


ABCDEFGHIJKL
1intervalresult
2Title1xxx-x
x
2
3Title1xxxxxx-0
4Title2x-x

x

x

3
5Title2x-x

x

x
3
6Title3x


x


x


3
7Title3x


x
x


x

4
8Title6-x





x

2

<tbody>
</tbody>

I tried doing this using a separate table to the right of the original. It contains TRUE/FALSE values for each cell showing whether that cell is preceded by 'interval' number of empty cells. Each cell in the second table has a formula looking something like this (example for cell C2):
=AND(CELL("contents",C2)>0,COUNTIF(INDIRECT(ADDRESS(ROW(),COLUMN(C2)-B2)&":"&ADDRESS(ROW(),COLUMN(C2)-1)),"x")=0)

Then I find the last occurrence of such a cell (giving me the beginning of the last chain):
=SUMPRODUCT(MAX(COLUMN(C2:K2)*(TRUE=C2:K2)))

And then I simply use COUNTIF() to get the number of 'x' cells from the beginning of the chain to the end.

There is however one issue with this method - when the 'interval' is large, the first function will look at too many cells to the left including non-existing cells outside of the spreadsheet. For example, in the last column in the given table, that function will count the number of 'x' cells from two rows to the left of the sheet to row C, which is an invalid interval.

I found this thread which uses the function I might need:
https://www.mrexcel.com/forum/excel-questions/550976-counting-consecutive-cells-value.html
... But I don't know how to apply it to my situation because I wait to look for cells that can be separated and don't contain numbers
 
OK, here's a non-volatile version:

Excel 2012
ABCDEFGHIJKLM
1intervalresult
2Title1xxxxx2
3Title1xxxxxx0
4Title2xxxxx4
5Title2xxxx3
6Title3xxx3
7Title3xxxx4
8Title6xx2

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet2

Array Formulas
CellFormula
M2{=IFERROR(COUNTIF(INDEX(A2:K2,1,MAX(IFERROR(IF(FREQUENCY(IF(C2:K2="",COLUMN(C2:K2)),IF((C2:L2="x")+(COLUMN(C2:L2)=COLUMN(L2)),COLUMN(C2:L2)))>B2-1,IFERROR(SMALL(IF((C2:L2="x")+(COLUMN(C2:L2)=COLUMN(L2)),COLUMN(C2:L2)),{1;2;3;4;5;6;7;8}),"")),0))):K2,"x"),0)}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



It's pretty ugly, but seems to work. Lightly tested, it works on the sample sheet, plus some more cases I tried. Note that it requires a column (L) after the last data column. It doesn't matter what's in it, you can hide it if you want. Also the array constant from 1-8 should have as many values as columns in the data range. Frankly, I'd stick with the volatile version, unless there are some severe calculation issues on the spreadsheet.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,214,559
Messages
6,120,194
Members
448,951
Latest member
jennlynn

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