Как переместить строку в столбец на основе идентификатора столбца - PullRequest
0 голосов
/ 13 декабря 2018

Я хочу преобразовать строки в столбцы на основе столбца A. Может ли кто-нибудь помочь мне с формулой или любой настройкой?

enter image description here

1 Ответ

0 голосов
/ 14 декабря 2018

Я не думаю, что возможно создать что-то подобное с формулой 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
...