需要做的事情背后的基本思想是:
- 获取要创建的新列数(例如
Total Rows/#rows per column
)
- 对于要制作的每个新列,从
nth + 1
行剪切到最后一行,其中nth
行是您想要的每列的行数(在您的情况下为 50)。
- 将剪切的行粘贴到其中包含数据的最后一列的右侧
- 重复直到完成所有列
这是我整理的一些示例代码,可帮助您入门。这假设您的数据从单元格 A1 开始,并且每列最多应包含 10 行。您可以更改该rowsToSkip
值以满足您的需求。另外,请注意,这是为了帮助您入门,需要更多测试。按照您认为合适的方式更改它:
Public Sub MakeColumnsFromRows()
Dim totalCutsToMake As Integer
Dim currentColumn As Integer
Dim currentCut As Integer
Dim rowsToCut As Integer
Sheets(1).Activate
rowsToSkip = 10
totalCutsToMake = (ActiveSheet.UsedRange.Rows.Count / rowsToSkip)
currentColumn = 1
Dim RowCount As Integer
For currentCut = 1 To totalCutsToMake
RowCount = Cells(Rows.Count, currentColumn).End(xlUp).Row
Range(Cells(rowsToSkip + 1, currentColumn), Cells(RowCount, currentColumn + 1)).Select
Selection.Cut
Cells(1, currentColumn + 2).Select
ActiveSheet.Paste
currentColumn = currentColumn + 2
Next
End Sub
它的作用是首先找出要创建多少新列,然后从每列的第 11 行切到最后一行,然后将这些值粘贴到包含数据的最后一列之后。它会这样做,直到所有新列都已创建。请注意,这会留下每列 10 行数据。要将其更改为 50,您只需将rowsToSkip
变量更改为50
.
这是之前和之后的屏幕截图:
前
后