将数据从一列重新排列为多行

时间:2018-11-26 04:26:20

标签: excel text

我有一个文本文件,它是一个很长的列,看起来像这样:

Title_1
Data_1
Data_2
Data_3
Title_2
Data_1
Data_2
Data_3
Data_4
Data_5
Title_3
Data_1
Title_4
Title_5
Data_1
Data_2

我想将这些数据重新排列成行和列:

Title_1     Data_1     Data_2     Data_3      
Title_2     Data_1     Data_2     Data_3     Data_4     Data_5 
Title_3     Data_1     
Title_4     
Title_5     Data_1     Data_2     

请注意,'title'始终以'N'开头,后跟其他字母/数字。

1 个答案:

答案 0 :(得分:1)

Sub TitlesAndData()

    Dim lrows As Long
    Dim counterRow As Long, counterCol As Long

    lrows = Cells(Rows.Count, "A").End(xlUp).Row
    counterRow = 0
    counterCol = 3

    For i = 1 To lrows
        If Left(Cells(i, 1), 1) = "N" Then
            counterRow = counterRow + 1
            counterCol = 3
            Cells(counterRow, 3).Value = Cells(i, 1).Value
        Else
            counterCol = counterCol + 1
            Cells(counterRow, counterCol).Value = Cells(i, 1).Value
        End If
    Next

End Sub

enter image description here