How to convert a long form table to wide form table in Excel?

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

A picture is worth a thousand words. Let's say in one sheet I have the following table:

Using this information, I want to programatically generate the table like this(sort of un-melting the long table into the wide form) in another sheet:

How can you achieve this?

回答1:

Using VBA:

Range("G1:K99").Clear For Each xx In Range("A:A")     If xx.Value = "" Then Exit Sub     Range("G1").Offset(xx.Value, 0) = xx.Value     For e = 1 To 99         If Range("G1").Offset(xx.Value, e) = "" Then             Range("G1").Offset(xx.Value, e) = xx.Offset(0, 1).Value             Exit For         End If     Next Next 

The table it's created from column "G". If you want another sheet:

Sheets(2).Range("G1: ... 

add the Sheets before ...

Without VBA, following the scheme:

Adding the formulas:

M2 -> =IFERROR(MATCH(L2;$A$1:$A$8;);"") N2 -> =IFERROR(MATCH(L2;INDIRECT("$A" & (M2+1) & ":$A$8");)+M2;"") O2 -> =IFERROR(MATCH(L2;INDIRECT("$A" & (N2+1) & ":$A$8");)+N2;"") P2 -> =IFERROR(INDEX($B$1:$B$8;M2);"")          Autocomplete also columns to R 

and Autocomplete ...



回答2:

I can see another approach as well. Add two columns Count and Key.

Add the formulas and expand:

C2 -> =COUNTIF($B$2:B2;B2) D2 -> =B2&"|"&C2 G2 -> =IFERROR(INDEX($A:$A;MATCH($F2&"|"&COLUMN(A1);$D:$D;0));"") 


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