Concatenate multiple values from table row into one cell

BraveDaveIA

New Member
Joined
Jan 19, 2016
Messages
8
I hope someone can help me with this, seemingly simple(?), need:

I need a formula to search through a table's row for multiple instances of a certain lookup value, then retrieve an associated retrieval value from a different row in that table, and concatenate all those retrieval values into a single cell, delimited by commas.

ABCDEF
1OrangesApplesPearsBananasGrapes
2Oranges, Pearsyy
3Apples, Bananas, Grapesyyy
4Apples, Pearsyy
5Oranges, Apples, Bananasyyy

<tbody>
</tbody>

In the sample table above, the formula would reside in cells A2 through A5. It would be pulling the retrieval values from row 1, based on a "y" appearing in any of the cells B2:F5.

It is not necessary the concatenated retrieval values appear alphabetized.

In reality, my tables will be up to 20 columns and up to 2,000 rows.

Here's hoping there's a way.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
This is a very long-winded way to do this, but it works, so hopefully someone can give you something better...
A​
B​
C​
D​
E​
F​
1​
OrangeApplePearBananaGrape
2​
Orange,Pearyy
3​
Apple,Banana,Grapeyyy
4​
Apple,Pearyy
5​
Orange,Apple,Bananayyy
A2=SUBSTITUTE(TRIM(IF(B2="y",B$1,"")&" "&IF(C2="Y",C$1,"")&" "&IF(D2="y",D$1,"")&" "&IF(E2="Y",E$1,"")&" "&IF(F2="y",F$1,""))," ",",")
copied down
 
Upvote 0
Here is another (quicker??) way to do this, using a helper table...
H​
I​
J​
K​
L​
M​
1​
OrangeApplePearBananaGrape
2​
Orange,PearOrangePear
3​
Apple,Banana,GrapeAppleBananaGrape
4​
Apple,PearApplePear
5​
Orange,Apple,BananaOrangeAppleBanana
6​
With your data as shown in my post above,
I2=IF(B2="Y",I$1,"")
copied down and across

Then...
H2=SUBSTITUTE(TRIM(I2&" "&J2&" "&K2&" "&L2&" "&M2)," ",",")
copied down
copied down
 
Upvote 0
This is a very long-winded way to do this, but it works, so hopefully someone can give you something better...
A​
B​
C​
D​
E​
F​
1​
OrangeApplePearBananaGrape
2​
Orange,Pearyy
3​
Apple,Banana,Grapeyyy
4​
Apple,Pearyy
5​
Orange,Apple,Bananayyy

<tbody>
</tbody>

A2=SUBSTITUTE(TRIM(IF(B2="y",B$1,"")&" "&IF(C2="Y",C$1,"")&" "&IF(D2="y",D$1,"")&" "&IF(E2="Y",E$1,"")&" "&IF(F2="y",F$1,""))," ",",")
copied down

Thanks, FDibbens.

I do have a few dozen tables, each with varying quantities of columns (up to ~20). With this formula, I'll have to add the concatenation for every column in each table. (Then again, I was already prepared for the fact that I'd have to adjust the range notation in each formula to address the varying quantities of columns.)

Thanks again.
 
Upvote 0
If you have the latest version of Excel 2016 with the new functions, you can use this formula in A2:

=TEXTJOIN(", ",TRUE,IF(B2:F2="y",$B$1:$F$1,""))
confirmed with Control+Shift+Enter, and copy down as needed.

If you don't have the latest version, you'll have to do something long-winded like Ford suggests, or use VBA.
 
Upvote 0
You can create your own UDF with VBA. Here is a suggestion:

Code:
Function JoinFruits(Rng1 As Range, Rng2 As Range) As String
    Dim R1 As Range
    For Each R1 In Rng1
        If Cells(Rng2.Row, R1.Column) = "y" Or Cells(Rng2.Row, R1.Column) = "Y" Then _
            JoinFruits = JoinFruits & R1.Value & ", "
    Next
    If Right(JoinFruits, 2) = ", " Then JoinFruits = Left(JoinFruits, Len(JoinFruits) - 2)
End Function

Rng1 = the header of the table, Rng2 = the current row

For your example in A2 write: =JoinFruits($B$1:$F$1,B2:F2) and fill down the rest cells of column A.

I hope this will work for the rest of your tables.
 
Last edited:
Upvote 0
If you have the latest version of Excel 2016 with the new functions, you can use this formula in A2:

=TEXTJOIN(", ",TRUE,IF(B2:F2="y",$B$1:$F$1,""))
confirmed with Control+Shift+Enter, and copy down as needed.

If you don't have the latest version, you'll have to do something long-winded like Ford suggests, or use VBA.

Arrgh! 2010.
 
Upvote 0
Beauty? That works very well. Thanks.

Now one other slight tweak:
Can the comma+space be made to occur even if there is only one retrieval value? (E.g., "Oranges, ")
I would not want the comma+space to appear if there are no "Y"'s. I.e., no retrieval values apply.
 
Upvote 0
If you always want a ", " at the end of the string, then delete the "If Right(JoinFruits, 2) = ", " Then JoinFruits = Left(JoinFruits, Len(JoinFruits) - 2)".

If a row has no Y/y, then the function returns "".

Is that you want?
 
Upvote 0

Forum statistics

Threads
1,215,217
Messages
6,123,670
Members
449,115
Latest member
punka6

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top