[英]Macro code to copy a cell and paste it to the next 2 cells
嘗試寫一個宏來復制一個單元格並將其粘貼到接下來的三個單元格中。然后復制第五個單元格並將其粘貼到行中的第6個,第7個和第8個單元格。我在行中有大量數據需要以這種方式復制粘貼。 到目前為止我有這樣的事情。
Sub copycell()
for i= 1 to 1000
Cells(1, i).Select
Selection.copy
Cells(1,i+1).Select
Selection.Paste
end if
End sub
next i
嘗試
Sub copycell()
For i = 1 To 1000 Step 4
Cells(1, i).Copy
Range(Cells(1, i + 1), Cells(1, i + 3)).PasteSpecial
Next i
End Sub
或者干脆
Sub copycell()
For i = 1 To 100 Step 4
Range(Cells(1, i + 1), Cells(1, i + 3)).Value = Cells(1, i).Value
Next i
End Sub
Step
關鍵字用於為循環的計數器變量指定不同的增量。 Step
更改用於遞增計數器的值。 在這種情況下, i
將增加4,即從1 to 5
然后增加到9
再增加到13
等等......
上面的代碼會將一行中的單元格復制到右側。
如果要在列中向下復制單元格,則可以使用以下代碼。
Sub copycell()
For i = 1 To 100 Step 4
Range(Cells(i + 1, 1), Cells(i + 3, 1)) = Cells(i, 1)
Next i
End Sub
在這里,每個循環之間有4個步驟,以便從第一個單元移動到第五個單元。
Sub copycell()
for i= 1 to 1000 step 4
Cells(1,i+1) = Cells(1, i)
Cells(1,i+2) = Cells(1, i)
Cells(1,i+3) = Cells(1, i)
next i
End sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.