我有个问题。
我想找到另一个文件和工作表中的最后一行,并从A-AD复制最后3行,但"C"列中有"0"的行除外。我想复制的行数始终为3。下面的代码有问题,因为它总是最后只复制一行。
Sub AB ()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim numCopied As Long
Dim baseWB As Workbook, baseWS As Worksheet
Dim spWB As Workbook, spWS As Worksheet
Set baseWB = ThisWorkbook
Set baseWS = ActiveSheet
lastRow = spWS.Cells(spWS.Rows.Count, "D").End(xlUp).Row
numCopied = 0
For i = lastRow To lastRow - 8 Step -1
' Sprawdź, czy w kolumnie C jest 0
If spWS.Cells(i, "C").Value <> 0 Then
spWS.Range(spWS.Cells(i, "A"), spWS.Cells(i, "AD")).Copy
numCopied = numCopied + 1
End If
If numCopied = 3 Then
Exit For
End If
Next i
baseWB.Sheets("Sheet1").Range("E5").PasteSpecial xlPasteValues
spWB.Close SaveChanges:=False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
1条答案
按热度按时间sqougxex1#
循环内的副本覆盖了前一个副本。除非使用并集,否则它们不是相加的。