![](/img/trans.png)
[英]Convert columns with multiple rows of data to rows with multiple columns in Excel.
[英]Convert multiple columns into rows in Excel
试试这个代码
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.