这是我脚本的一部分,轻松循环将某些行从一张纸复制到另一张纸:
a = 3
With Sheets("ATD")
Do While .Range("A" & a) <> ""
If .Cells(a, 6).Value = "x" And .Cells(a, 8).Value = "y" Then
.Range(Cells(a, 1), Cells(a, 10)).Copy
Sheets("ART").Range("A" & Sheets("ART").Range("A" & Rows.Count).End(xlUp).row + 1).PasteSpecial xlPasteValues
End If
a = a + 1
Loop
End With
它几乎每次上.Range(Cells(a, 1), Cells(a, 10)).Copy
线都会失败(运行时错误'1004':应用程序定义的错误或对象定义的错误)。当我添加这样的.Select
命令时:
a = 3
With Sheets("ATD")
Do While .Range("A" & a) <> ""
If .Cells(a, 6).Value = "x" And .Cells(a, 8).Value = "y" Then
.Select
.Range(Cells(a, 1), Cells(a, 10)).Copy
Sheets("ART").Range("A" & Sheets("ART").Range("A" & Rows.Count).End(xlUp).row + 1).PasteSpecial xlPasteValues
End If
a = a + 1
Loop
End With
一切正常。
我知道我可以改变.Copy
类似的东西
Sheets("ATD").Range(Cells(a, 1), Cells(a, 10)).Value = Sheets("ART").Range(Cells(b, 1), Cells(b, 10)).Value
但是我还有一个问题。如果.Copy
功能需要,我要复制的单元格在当前选择的工作表中,还是我在这里丢失了什么?
您可以尝试:
.Range(.Cells(a, 1), .Cells(a, 10)).Copy
这些点非常重要,因为它们Cells
使用行中的“Sheet
对象”设置引用当前电流With Sheets("ATD")
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句