Table of data want to group, transpose matching values same line

偶尔善良 提交于 2021-01-27 21:06:22

问题


I have a table of data in Excel. Column A contains Names, Column B contains their interest. Each interest has a separate row. I want to take the data from this table and have a single row with the name of the customer and a column for each of their interests. IE RAW Data:

I am looking to take the 4000 row table and grouping by the name. I am unsure how many times each name appears in the list (Once or Fifty times) but I want the interests placed on a single row with each interest in a separate column EG Desired Data:

I have tried the standard transpose....html table....and pivot tables but it will put the interests all in a row along the top regardless if the customer is interested or not and using a record count T/F that means the data sheet in harder to understand then if I leave it as one block and sort by name

Sure I am not alone with this but all searches for the past 2 hrs keep returning pivot/transpose or duplicate items. Any is appreciated


回答1:


If you don't want to use VBA, you could first add a column, for instance in column C, with the title "InterestNum."

In C2, just put 1.

In C3, put =COUNTIF($A$2:$A2, $A3) + 1. This will find the number interest it is for the person.

Make a lookup column, for instance in column D. In D2, put =A2&C2

Then, make a list of all the people. I assume that you put this list starting in cell A2 of a new sheet. Then put headers starting in B1 so that B1 contains the title "1" and C1 contains the title "2" standing for the interest number and as many columns as you wish.

Then in Cell B2, put the formula =IF(ISNA(MATCH($A2&B$1,data!$D$2:$D$5,0)),"",INDEX(data!$B$2:$B$5,MATCH($A2&B$1,data!$D$2:$D$5,0)))

This assumes that your original data is in the data tab. I only tested with 4 rows, so you would need to change $D$2:$D$5 to have as many rows as you do. This works by looking up a combination of the name and interest number. It first checks to see if that combination exists in the data. If not, it leaves that interest blank. If so, it finds the actual interest by going to the same row of the lookup.




回答2:


First remove duplicates using standard excel functionality to prevent having the same interest twice for a person.

Now, you could of course use VBA and perform exactly what you need. However, I suggest that you use the pivot table. If your data looks something like this...

... just use "Insert | Pivot table" and insert a pivot table to a new worksheet. Then, configure the columns as follows:

Et voilà, there you have all your interests listed only once and if a person shares an interest, there is a "1".

If you would rather use VBA, just comment and I will edit my answer.



来源:https://stackoverflow.com/questions/39508852/table-of-data-want-to-group-transpose-matching-values-same-line

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