我目前正在运行一个循环,它选择一个范围,复制然后将值粘贴到所需的列中:
Set wks6 = wkb.Sheets.Add
With wks6
Dim rstCP As Recordset
Dim firstCP As Boolean
Dim previousPot As Integer
Set rstCP = CurrentDb.OpenRecordset("FUT_CR_Capacity_Count")
firstCP = False
previousPot = 0
Do While Not rstCP.EOF
If firstCP = False Then
.Range("N3", "AA3").Copy
.Range("AB" & rstCP![COuntWC] + 3).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
previousPot = previousPot + rstCP![COuntWC] + 3
firstCP = True
Else
.Range("N" & previousPot + 1 & ", AA" & previousPot + 1 & "").Copy
Debug.Print "N" & previousPot + 1 & ",AA" & previousPot + 1 & ""
.Range("AB" & rstCP![COuntWC] + previousPot + 1).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Debug.Print "AB" & rstCP![COuntWC] + previousPot + 1
previousPot = previousPot + rstCP![COuntWC] + 1
End If
rstCP.MoveNext
Loop
End With
所以循环的第一部分 ( If firstCP = False Then
) 运行得非常好;它会自动复制 N3 到 AA3 范围内的值,进而自动粘贴 AA13 到 AO13 范围内的值。但是,当循环继续执行所有其他增量(if 语句的 else 部分)时,它将部分粘贴 AA15 到 AB15 列中的值。
注意:我尝试在粘贴中指定范围:
.Range(AA#,AO#).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
...但这不起作用。