Home » excel » excel – How to append to text based on condition

excel – How to append to text based on condition

Posted by: admin May 14, 2020 Leave a comment


How to create a formula in result column based on the values of the previous column
enter image description here

if values in any previous columns (from c1 to c5) is greater than zero, I want to add it to the result.

How to&Answers:

If you happen to have access to TEXTJOIN function you can use the following:

enter image description here

Formula in F2:


Note 1: It’s an array formula and needs to be confirmed through CtrlShiftEnter

Note 2: Without access to TEXTJOIN this will become a much more painfull process, but for just the 5 columns you can try =IF(A2>0,A$1&",","")&IF(B2>0,B$1&",","")&IF(C2>0,C$1&",","")&IF(D2>0,D$1&",","")&IF(E2>0,E$1&",",""), however you’ll have to expand to get rid of the trailing comma

If both the above options are not feasible, your best bet will be to write a UDF. For this exercise, you can write a rather simple one:

Public Function TextJoin2(rng1 As Range, rng2 As Range) As String

With Application
    Dim arr As Variant: arr = .Evaluate("IF(" & rng1.Address & ">0," & rng2.Address & ","" "")")
    TextJoin2 = Replace(.Trim(Join(arr, " ")), " ", ",")
End With

End Function

Call in F2 like: =TEXTJOIN2(A2:E2,A$1:E$1) and drag down.


You can use =CONCAT(IF(A2:E2>0;$A$1:$E$1&",";"")) as array formula, confirm with ShiftCtrlEnter.
Put this formula in F2 cell in your example. Expand that formula to whole range
What remains here is to remove comma on the end of result string.

That full formula would be like this