简体   繁体   中英

concatenate multiple matches in excel

Please see below

图片

I want to concatenate 'comments' in table 2 into table 1 as shown in the series of images without using TEXTJOIN() or macros. Only using regular excel functions

There is no simple solution without using UDF or helper columns. I would suggest using UDF formula which is simple to implement and use in worksheets. To use this approach, please enter this code in your regular module (module1).

Function Lookup_concat(Search_string As String, _
Search_in_col As Range, Return_val_col As Range)

Dim i As Long
Dim result As String

For i = 1 To Search_in_col.Count
If Search_in_col.Cells(i, 1) = Search_string Then
result = result & " " & Return_val_col.Cells(i, 1).Value
End If
Next

Lookup_concat = Trim(result)

End Function

now you can use this UDF just like regular worksheet formula. Enter this formula =Lookup_concat(G3,$D$3:$D$12,$E$3:$E$12) in cell I3 and drag it to the bottom.

在此处输入图片说明

in case you want to use only regular formulas, you will need to enter this formula =IFERROR(INDEX($D$3:$E$12, SMALL(IF(($G3=$D$3:$D$12), ROW($D$3:$D$12)-MIN(ROW($D$3:$D$12))+1, ""),COLUMNS($A$1:A1)), 2),"") in cell K3 using CTRL+SHIFT+ENTER combination since it is an array formula. Now drag formmula to the right and down(Estimate how far to the righ your formula needs to go in order to catch all unique values).

在此处输入图片说明

Then enter this formula =CONCATENATE(K3," ",L3," ",M3," ",N3," ",O3," ") in cell J3 and drag it to the bottom (adjust formula to estimated number of unique values).

在此处输入图片说明

You will need to add a helper column to achieve your goal.

Assuming you have the helper column C and this is the array formula (means you have to click Ctrl + Shift + Enter altogether) you should try:

{=IF(OR(ROW(C1)=1,MAX(--($A$1:A1=A2)*ROW($A$1:A1))=0),B2,INDEX($C$1:C1,MAX(--($A$1:A1=A2)*ROW($A$1:A1)))&", "&B2)}

Now at column G assuming this is the place you want to get your outcome, you can enter this array formula (means you have to click Ctrl + Shift + Enter altogether):

{=IFERROR(INDEX($A$2:$C$11,MAX(--($A$2:$A$11=E2)*ROW($A$2:$A$11))-1,3),"")}

This way you should get the results you are expecting.

There's a simple way to do this. :) Please see this Google sheet for a working example.

You can use the FILTER and JOIN functions to achieve this:

=iferror(join(", ", filter(E$3:E$12, D$3:D$12 = G3)))

In the above example the FILTER function will look at cells D3:D12 and try to find rows matching the value in G3 . For the matching rows, the FILTER function returns the values from cells E3:E12 as an array.

JOIN is used to join the array items together with a comma in between.

Finally, IFERROR gets rid of N/A errors resulting from FILTER not matching anything.

(Kudos to original answer here https://stackoverflow.com/a/23367059/36817 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM