我在脚本编写方面经验不足,我正在尝试使用VBA在excel中重新排列大量数据。 该脚本只通过内部循环,不运行外部循环。任何指针为什么?
谢谢!
Row = 6
Column = 5
Destinationrow = 6
Destinationcolumn = 19
Do While Column <= 16 And Destinationcolumn <= 30
Do While Row <= 561 And Destinationrow <= 92
ActiveSheet.Cells(Row, Column).Select
Selection.Copy
ActiveSheet.Cells(Destinationrow, Destinationcolumn).Select
ActiveSheet.Paste
Row = Row + 9
Destinationrow = Destinationrow + 1
Loop
Column = Column + 4
Destinationcolumn = Destinationcolumn + 1
Loop
答案 0 :(得分:2)
它看起来很好,除了你没有在内循环结束时重置你的行。也许它应该是:
Column = 5
Destinationcolumn = 19
Do While Column <= 16 And Destinationcolumn <= 30
Row = 6
Destinationrow = 6
Do While Row <= 561 And Destinationrow <= 92
ActiveSheet.Cells(Row, Column).Select
Selection.Copy
ActiveSheet.Cells(Destinationrow, Destinationcolumn).Select
ActiveSheet.Paste
Row = Row + 9
Destinationrow = Destinationrow + 1
Loop
Column = Column + 4
Destinationcolumn = Destinationcolumn + 1
Loop
答案 1 :(得分:0)
你必须在第一个循环中初始化行循环变量:
Column = 5
Destinationcolumn = 19
Do While Column <= 16 And Destinationcolumn <= 30
Row = 6
Destinationrow = 6
Do While Row <= 561 And Destinationrow <= 92
ActiveSheet.Cells(Row, Column).Select
Selection.Copy
ActiveSheet.Cells(Destinationrow, Destinationcolumn).Select
ActiveSheet.Paste
Row = Row + 9
Destinationrow = Destinationrow + 1
Loop
Column = Column + 4
Destinationcolumn = Destinationcolumn + 1
Loop
另外,你可以用以下代码替换4个复制和粘贴行:
ActiveSheet.Cells(Destinationrow, Destinationcolumn) = ActiveSheet.Cells(Row, Column).Value