VBA代码将一个多个数据列转换为多个列

时间:2017-02-08 11:20:49

标签: vba excel-vba excel

您好我有一列65000行的股票价格为一列,我想在第二张图片中调整它,任何人都知道如何用vba编码它?谢谢!

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

假设数据结构总是相同的(12个月的数据和3行的数据集ID)。更改代码行9上的工作表名称以适合您的工作表名称

Sub TRANSPOSE_DATA() Dim lRow, i, x As Long '------------------ With ThisWorkbook 'ADD OUTPUT WORKSHEET .Sheets.Add After:=.Sheets(.Sheets.Count) 'add a sheet for output .ActiveSheet.Name = "OUTPUT" 'sheet rename 'COPY DATA With .Sheets("Hoja1") 'Change sheet name for yours lRow = .Range("A1048576").End(xlUp).Row 'last row definition .Range("A1:B15").Copy Destination:=ThisWorkbook.Sheets("OUTPUT").Range("A1") 'First dataset copy (including headers) x = 3 'first iteration column definition For i = 16 To lRow - 14 .Range("B" & i & ":B" & i + 14).Copy Destination:=ThisWorkbook.Sheets("OUTPUT").Cells(1, x) 'copy data from each iteration x = x + 1 'transpose 1 column for next iteration i = i + 14 'transpose 12 months plus header rows for next iteration Next i End With End With End Sub