如何根据ID列将行转置为列

时间:2018-12-13 18:05:37

标签: excel-formula excel-2010

我想将行转换为基于A列的列。有人可以通过公式或任何设置帮助我吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为用excel公式无法创建类似的东西。我尝试创建一个可能有助于使用VBA的代码。

尝试:

Option Explicit

Sub test()

    Dim LR As Long, LC As Long, i As Long, No As Long

    With ThisWorkbook.Worksheets("Sheet1")

        LR = .Cells(.Rows.Count, "A").End(xlUp).Row

        .Cells(LR + 3, 1).Value = "Event_Id"
        .Cells(LR + 4, 1).Value = "1"
        .Cells(LR + 5, 1).Value = "2"
        .Cells(LR + 6, 1).Value = "3"

        For i = 2 To LR
            No = .Cells(i, 1).Value

            If No = "1" Then

                LC = .Cells(LR + 4, .Columns.Count).End(xlToLeft).Column

                If .Cells(LR + 3, LC + 1).Value = "" Then
                    .Cells(LR + 3, LC + 1).Value = "Unit_Id"
                End If
                    .Cells(LR + 4, LC + 1).Value = .Cells(i, 2).Value

                If .Cells(LR + 3, LC + 2).Value = "" Then
                    .Cells(LR + 3, LC + 2).Value = "Qty"
                End If
                    .Cells(LR + 4, LC + 2).Value = .Cells(i, 3).Value

            ElseIf No = "2" Then
                LC = .Cells(LR + 5, .Columns.Count).End(xlToLeft).Column

                If .Cells(LR + 3, LC + 1).Value = "" Then
                    .Cells(LR + 3, LC + 1).Value = "Unit_Id"
                End If
                    .Cells(LR + 5, LC + 1).Value = .Cells(i, 2).Value

                If .Cells(LR + 3, LC + 2).Value = "" Then
                    .Cells(LR + 3, LC + 2).Value = "Qty"
                End If
                    .Cells(LR + 5, LC + 2).Value = .Cells(i, 3).Value

            ElseIf No = "3" Then
                LC = .Cells(LR + 6, .Columns.Count).End(xlToLeft).Column

                If .Cells(LR + 3, LC + 1).Value = "" Then
                    .Cells(LR + 3, LC + 1).Value = "Unit_Id"
                End If
                    .Cells(LR + 6, LC + 1).Value = .Cells(i, 2).Value

                If .Cells(LR + 3, LC + 2).Value = "" Then
                    .Cells(LR + 3, LC + 2).Value = "Qty"
                End If
                    .Cells(LR + 6, LC + 2).Value = .Cells(i, 3).Value

            End If

        Next i

    End With

End Sub