vba编码重新排列数据集

时间:2017-09-08 08:28:16

标签: excel vba excel-vba

我有一些范围的数据集,每个7x12个单元格都按列排列,所以我想将它重新排列成行。
某些范围集可能为空,因此我将忽略该范围集。附图显示了当前结果和所需结果。
谢谢。

示例图片:
enter image description here

1 个答案:

答案 0 :(得分:0)

你可以尝试一下。请查看列并更新您的方案

Public Sub DataRearrange()
    Dim LastRowInSet As Long, LastCol As Long, LastRowInTransposedData As Long, ColumnIncrease As Long
    Dim j As Long

    ' Change this to the width of your repeating data set
    ColumnIncrease = 4
    ' Change this to your relevant Sheet Name
    With Sheets("InsertYoursheetNameHere")
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        ' There's no point doing it if there's only one set so lets Exit
        If LastCol <= ColumnIncrease Then Exit Sub

        For j = ColumnIncrease + 1 To LastCol Step ColumnIncrease
            LastRowInSet = .Cells(.Rows.Count, j).End(xlUp).Row
            LastRowInTransposedData = .Cells(.Rows.Count, 1).End(xlUp).Row
            Range(.Cells(LastRowInTransposedData + 1, 1), .Cells(LastRowInTransposedData + LastRowInSet, ColumnIncrease)).Value2 = Range(.Cells(1, j), .Cells(LastRowInSet, j + ColumnIncrease - 1)).Value2
            Range(.Cells(1, j), .Cells(LastRowInSet, j + ColumnIncrease - 1)).Clear
        Next j
    End With
End Sub