[英]Convert multiple columns into rows in Excel
How can I transform a file from one format to another: basically Week columns with unit sales become two columns with multiple rows and first few columns (with customer etc info) have multiple rows for unit sales columns columns. 如何将文件从一种格式转换为另一种格式:基本上,具有销售单元的“周”列变为具有多行的两列,而前几列(包含客户等信息)具有“销售单元”列的多行。
From 从
I think there is a way to do it in Power Query? 我认为Power Query有办法做到这一点? I am not a very advanced user so would greatly appreciate step by step how to do it :)
我不是一个非常高级的用户,所以非常感谢您逐步实现它:)
Unless there is a macro I can run? 除非有宏我可以运行?
Thanks 谢谢
Ewa 伊娃
Try this code 试试这个代码
Sub Test()
Dim a As Variant
Dim b As Variant
Dim i As Long
Dim j As Long
Dim k As Long
Dim x As Long
a = Sheets("Sheet1").Range("A1:L" & Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row).Value
ReDim b(1 To UBound(a, 1) * 8, 1 To 6)
j = 1
For i = 2 To UBound(a, 1)
For k = 5 To UBound(a, 2)
For x = 1 To 4
b(j, x) = a(i, x)
Next x
b(j, 5) = a(1, k)
b(j, 6) = a(i, k)
j = j + 1
Next k
Next i
With Sheets("Sheet1").Range("O1")
.Resize(, UBound(b, 2)).Value = Array("Account", "Code", "Product", "Segment", "Week", "Unit Sales")
.Offset(1).Resize(UBound(b, 1), UBound(b, 2)).Value = b
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.