excelexcel-formula

How to write this Excel in-cell formula "properly" and more compact?


Checking per row if column D = true, then returning as string the corresponding cells in column B and C.

I can write the below manually just fine, but it's not great for adding extra rows later and I know there has to be a better way to do it, I just don't know what phrase to search for to help me find out.

[...]
&if(D10=true(),B10&": "&C10&char(10),"")
&if(D11=true(),B11&": "&C11&char(10),"")
&if(D12=true(),B12&": "&C12&char(10),"")
&if(D13=true(),B13&": "&C13&char(10),"")
&if(D14=true(),B14&": "&C14&char(10),"")
[...]

Solution

  • The following should work:

    =TEXTJOIN(CHAR(10), TRUE, IF(D10:D14=TRUE, B10:B14 & ": " & C10:C14, ""))
    

    And traditionally, just to make things more awkward for anyone trying to maintain this formula in the future:

    =LET(
        InputRange, B1:D5,
        Delimiter, CHAR(10),
        BColumn, INDEX(InputRange,,1),
        CColumn, INDEX(InputRange,,2),
        DColumn, INDEX(InputRange,,3),
        IsTrue, IF(DColumn=TRUE, BColumn & ": " & CColumn, ""),
        JoinText, TEXTJOIN(Delimiter, TRUE, IsTrue),
        JoinText
    )