The following two formulas return the desired results:
Rows:
=LEFT(CONCAT(IF(B6:L6="a",(B1:K1&","),"")),LEN(CONCAT(IF(B6:L6="a",(B1:L1&","),"")))-1)
Columns:
=LEFT(CONCAT(IF(L2:L11="a",(A2:A11&","),"")),LEN(CONCAT(IF(L2:L11="a",(A2:A11&","),"")))-1)
Now that's done with functions available in 2016. (I cannot test this part, but it might've been necessary to make them array formulas using {CSE}
entry.)
Obviously they are similar. The idea is to test the row (or column) for the value sought ("a" in this case) using a range as the base for the IF()
test. Return "" if FALSE, and the tricky part... how to return the values AND put commas in between them?
Curiously, if you specify the range to draw your output from AND add &","
to the end of it, you do NOT get something like 91923,
but rather Excel understands you'd like the comma added after every return. Two things: You have to put that string in parentheses or Excel can misunderstand what you are trying for. And the reason you don't get multiple commas in a row (like ",,9,,,,,19,23,,") is that the construction gives only returns for TRUE results so all the blanks that would lead to "blank," entries showing consecutive commas simply don't happen.
You DO end up with a comma at the end though, hence the LEFT()
that doubles the formula length, but does strip off that last comma.
Another interesting trick is not specifying the test range as the range for output but rather specifying the column (or row) headers as the output so that you get the desired output directly.
Nowadays (ultra-modern 2021...) we have TEXTJOIN()
and SPILL
functionality. So definitely no {CSE}
needed, and just a simple formula because TEXTJOIN()
will not have a comma at the end:
=TEXTJOIN(",",TRUE,IF(B6:L6="a",B1:L1,""))
In this one, those blank returns it seems could occur DO occur so you definitely need to specify TRUE for the second parameter in TEXTJOIN()
so as to keep from having the consecutive commas problem. You still need the test in order to decide which of the header cells are to return a value and which are not though. For the columns:
=TEXTJOIN(",",TRUE,IF(L2:L11="a",A2:A11,""))
Again, obviously very similar to the one for the rows.