Using Vlookup/Macros to Transpose

天大地大妈咪最大 提交于 2019-12-13 06:13:18

问题


In Excel, I have a unique listing of people and their various roles that they hold in separate columns within each row as following:

Name     Role     Role     Role     Role     Role
John         A         B           C        null       null
Jane         D         C         null        null       null
Lisa         A         B           C          F          G

I want to transpose all the roles for each unique 'Name' so that there is a row for each combination of Name - Role as follows:

Name    Role
John      A
John      B
John      C
Jane      D
Jane      C
Lisa      A
Lisa      B
Lisa      C
Lisa      F
Lisa      G

Is there any combination of functions or macros I can utilize to get this output in another sheet or the same spreadsheet? Even if the transposed rows includes null values, I can go in and delete all rows with blank values at the end. I have about 30+ users and many have 20+ roles that I'm trying to separate so this may save me time if there's an automated way to do it.


回答1:


In H2:

=OFFSET($A$1,CEILING((ROW()-1)/COUNTA($B$1:$F$1),1),0)

In I2:

=VLOOKUP(H2,$A$2:$F$4,COUNTIF(H$2:H2,H2)+1,FALSE)

Fill both down.




回答2:


This will list all the names and exclude nulls as long as the nulls exist for each person as the right most cells. (Meaning there is no pattern like A, B, Null, C, Null, D).

H2 = =COUNTA(B2:F2), drag down

A7 = =A2

A8 = =IF(COUNTIF($A$7:A7,A7)<VLOOKUP(A7,$A$2:$H$4,8,0),A7,OFFSET(A$1,MATCH(A7,$A$1:$A$4,0),0)), drag down for names * roles rows

B7 = =OFFSET($A$1,MATCH($A7,$A$1:$A$4,0)-1,COUNTIF($A$7:$A7,$A7)), drag down for names * roles rows

EDIT:

To eliminate column H, change the formula in A8 to the below:

=IF(COUNTIF($A$7:A7,A7)<COUNTA(OFFSET($A$1,MATCH(A7,$A$1:$A$4,0)-1,1,1,COUNTA($A$1:$F$1))),A7,OFFSET(A$1,MATCH(A7,$A$1:$A$4,0),0))


来源:https://stackoverflow.com/questions/38232626/using-vlookup-macros-to-transpose

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