我有兩個作業表。在一個名為“設備詳細資訊”的作業表中,我在 A 列的第 13 到 1000 行中有一組值。我想將這些值中的每一個,即 A13、A14、A15 等復制到另一個名為“作業表(2 )”從單元格 A2 開始。但是,訣竅是需要將第一個作業表中的 A13 復制到第二個作業表的 A2、A14 到 A8、A15 到 A14 中,每次以 6 為增量復制。以下是我的代碼,但它不起作用。它將第一條記錄從 A13 復制到 A2,但隨后就出錯了。請幫忙!
Sub CopyData2()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim srcws As Worksheet
Set srcws = wb.Worksheets("Equipment details")
Dim destws As Worksheet
Set destws = wb.Worksheets("Worksheet (2)")
Dim frstRec As Long
Dim k As Integer
Dim SrcRowNo As Integer
Dim DestRowNo As Integer
Dim myRange As Range
Set myRange = destws.Range("a2")
'Source sheet starting row
SrcRowNo = 13
'Destination sheet starting row
DestRowNo = 2
'Copy and paste first record into destination sheet
srcws.Cells(SrcRowNo, 1).Copy Destination:=destws.Cells(DestRowNo, 1)
frstRec = myRange.Row
For SrcRowNo = 13 To 50
For frstRec = 2 To 50
srcws.Cells(SrcRowNo 1, 1).Copy Destination:=destws.Cells(frstRec, 1)
Next frstRec
Next SrcRowNo
End Sub
uj5u.com熱心網友回復:
Sub CopyData2()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim srcws As Worksheet
Set srcws = wb.Worksheets("Equipment details")
Dim destws As Worksheet
Set destws = wb.Worksheets("Worksheet (2)")
Dim RowNo As Long
For RowNo = 0 To 987
srcws.Cells(RowNo 13, 1).Copy Destination:=destws.Cells(RowNo*6 2, 1)
Next RowNo
End Sub
uj5u.com熱心網友回復:
Option Explicit
Sub CopyData2()
Dim wb As Workbook, wsSrc As Worksheet, wsDest As Worksheet
Dim t0 As Single: t0 = Timer
Set wb = ThisWorkbook
Set wsSrc = wb.Worksheets("Equipment details")
Set wsDest = wb.Worksheets("Worksheet (2)")
' copy A13->A2, A14->A8, A15->A14
Const INCR = 6
Const START_ROW = 13
Const END_ROW = 1000
Dim arSrc, arDest, i As Long, j As Long
arSrc = wsSrc.Range("A" & START_ROW & ":A" & END_ROW).Value2
arDest = wsDest.Range("A2:A" & INCR * UBound(arSrc)).Value2
For i = 1 To UBound(arSrc)
j = 1 (i - 1) * INCR
arDest(j, 1) = arSrc(i, 1)
Next
wsDest.Range("A2").Resize(UBound(arDest)) = arDest
MsgBox "Done", vbInformation, Format(Timer - t0, "0.0 secs")
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/368746.html
