Lookup, Match and Concatenate

房东的猫 提交于 2019-12-12 18:05:07

问题


I need a formula/function to concatenate cell values from one column and multiple rows. The matching criteria is applied to a different column. Here is my example of what I have to do:

Islington | "Bunhill"   | EC2M  
Islington | "Bunhill"   | EC2Y  
Islington | "Bunhill"   | N1  
Barnet    | "Burnt Oak" | HA8  
Barnet    | "Burnt Oak" | NW7  
Barnet    | "Burnt Oak" | NW9  

The end result needs to look like this:

Islington | "Bunhill"   | EC2M, EC2Y, N1  
Barnet    | "Burnt Oak" | HA8, NW7, NW9 

Basically, I need to remove all duplicates from the second column, but save the data from the third column that is paired with each of the duplicates, and concatenate it in one cell.


回答1:


You can go through a process of steps using functions. Start with the UNIQUE function. Put this in a cell where it is convenient to list all the unique values of column B:

=UNIQUE(B:B)

Gets all the unique values in column B. Google Support - Unique Function

The result from the UNIQUE function will look like this:

Now that you have all the unique values from column B, you can use the FILTER function to retrieve all the rows that match that unique value.

=FILTER(D1:D6, B1:B6=A8)

The FILTER function lists all the results down the column, but you can use the CONCATENATE function to avoid that.

Results of FILTER function:

Results of CONCATENATE:

You will need to adjust the FILTER function to now use column D, rather than column C.

=CONCATENATE(FILTER(D1:D6, B1:B6=A8))

This solves the problem of getting data in multiple rows, but now there is no separator between the values.

To get around that problem, you can create a fourth column with a function that adds a comma to the end:

There is a problem with an extra comma on the end, which you can get rid of with the LEFT function:




回答2:


If not required too often it is quite practical without a script. Assuming EC2M is in C2, D1 is blank, and your data is sorted, in D2:

=if(B1=B2,D1&", "&C2,C2) 

and in E2, both formulae copied down to suit:

=B2=B3 

Select all, Ctrl+c, Edit, Paste special, Paste values only over the top and filter to select and delete rows with TRUE in ColumnE.



来源:https://stackoverflow.com/questions/28177879/lookup-match-and-concatenate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!