以下程式預定從 C2 複製到 C6
Option Explicit
Const kRowCount = 300
Sub Main()
Dim nI
For nI = 2 To 6
OneCopy (nI)
Next nI
End Sub
Sub OneCopy(ByVal pIdx As Integer)
Dim nRowBegin, nRowEnd
nRowBegin = 2 + (pIdx - 2) * kRowCount
nRowEnd = nRowBegin + kRowCount - 1
Sheets("SheetB").Select
Range("C" & pIdx).Select
Selection.Copy
Sheets("SheetA").Select
Range("B" & nRowBegin).Select
ActiveSheet.Paste
Range("B" & nRowBegin & ":B" & nRowEnd).Select
Selection.FillDown
End Sub
可以運用Range.Resize方式給值,會看起簡單些
Sub test()
Dim rngChk as Range
Dim rngFin as Range
Dim i%
Dim shtA as WorkSheet
Dim shtB As WorkSheet
Set shtA = WorkSheets("SheetA")
Set shtB = WorkSheets("SheetB")
set rngChk = shtB.Range([C2],[C2].End(xlDown))
set rngFin = shtA.[B2]
Application.ScreenUpdate = False
For i = 0 to rngChk.count - 1
rngFin.offset(i).resize(,300)=rngChk(i+1).value
next
end sub