Based on your sample - using the rows - I'd be inclined to keep a count of the number of items to be retrieved:
AO12:
=SUMPRODUCT(--(MID(AM12&REPT("c",RIGHT(AM12)="x"),ROW($1:$25),2)="xc"))
copied down to AO36
With the above in place - in terms of replicating your expected results
C12:
=IF((9-COLUMNS($C12:C12))>$AO12,"",($L12="x")+FIND("^^",SUBSTITUTE($AM12&REPT("c",$AJ12="x"),"xc","^^",1+COUNT($B12:B12)))-FIND("^^",SUBSTITUTE(REPT("c",$L12="x")&$AM12,"cx","^^",1+COUNT($B12:B12))))
copied across matrix C12:J36
note there is an error in your sample - row 23 should be 2 2 4 1 2 rather than 2 2 1 4 1 2
Bookmarks