我试图粘贴" Area1" 50次," Area2" 50次正好在" Area1"在同一列中。我的代码运行前50次,并在运行Area = 2时替换前50行。我如何制作" Area2"在" Area1"?下的新行开始谢谢:))
For Area = 1 To 2
For Row = 1 To 50
Sheets("A").Cells(Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
答案 0 :(得分:1)
执行此操作的一种方法是创建另一个变量并在第二个循环中递增它,每次+1。像这样:
For Area = 1 To 2
For Row = 1 To 50
myRow = myRow + 1
Sheets("A").Cells(myRow, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
答案 1 :(得分:1)
有很多方法可以做到这一点。其中一些将是:
For Area = 1 To 2
For Row = 1 To 50
Sheets("A").Cells((Area - 1) * 50 + Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
或
For Area = 1 To 2
For Row = (Area - 1) * 50 + 1 To (Area - 1) * 50 + 50
Sheets("A").Cells(Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
或将Area
更改为从零开始
For Area = 0 To 1
For Row = 1 To 50
Sheets("A").Cells(Area * 50 + Row, 2) = Sheets("A").Cells(2, 31)
Next Row
Next Area
或避免循环
Sheets("A").Cells(1, 2).Resize(50, 1) = Sheets("A").Cells(2, 31)
Sheets("A").Cells(51, 2).Resize(50, 1) = Sheets("A").Cells(2, 31)
或者,因为您要将相同的值复制到目标单元格,只需
Sheets("A").Cells(1, 2).Resize(100, 1) = Sheets("A").Cells(2, 31)