在Excel中将多个列转换为行

时间:2018-03-01 10:55:07

标签: excel excel-vba vba

如何将文件从一种格式转换为另一种格式:基本上,具有单位销售额的周列成为两列,其中包含多行,前几列(带有客户等信息)的单位销售列列有多行。

enter image description here

enter image description here

我认为在Power Query中有一种方法可以做到这一点?我不是一个非常高级的用户,所以一步一步地非常欣赏如何做到这一点:)

除非有可以运行的宏吗?

由于

埃娃

1 个答案:

答案 0 :(得分:0)

试试此代码

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