I have a sheet in Excel 365 with the columns A and B as shown below and I want to get columns C and D with some formula (not VBA!). That is, I want to repeat every Title for Count times and add a running number to it.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Title | Count | Running Title | Running Number |
| 2 | Anna | 3 | Anna | 1 |
| 3 | Ben | 2 | Anna | 2 |
| 4 | Anna | 3 | ||
| 5 | Ben | 1 | ||
| 6 | Ben | 2 |
For the Running Title, I found a somewhat arcane formula that looks somewhat unstable (relying on string processing). It basically uses the following formula to create an array that is {2,2,2,3,3} and XLOOKUP on that to get the titles.
=XLOOKUP(
IFERROR(
FILTERXML(
"<a><b>"&
SUBSTITUTE(TRIM(TEXTJOIN(" ",TRUE,REPT(ROW($A$2:$A$100)&" ",$B$2:$B$100)))," ","</b><b>")&
"</b></a>",
"//b"
),
""),
ROW($A$2:$A$100),
A2:A100
)
This works, but it would be great to see some less patchy formula (i.e. not relying on specifics on how Excel treats strings) for the purpose.
For the Running Number, I could use a formula like =IF($A2<>"",IF($C1=$C2,$D1+1,1),"") in cell D2 and extend it downwards by dragging the lower right corner downwards. This works, but I would love to see a spilling formula that only has to be defined in D2 and nowhere else.
In summary, is there a way to create columns C and D in the example above without having to define the size of the output (like I do by manually expanding the formula area) and without resorting to functions that depend on the internal workings of strings in Excel?



