这是我的脚本的一部分,轻松循环将一些行从一个工作表复制到另一个工作表:
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
函数需要,我要复制的单元格是当前选中的工作表,还是我在这里遗漏了什么?
答案 0 :(得分:3)
你可以尝试:
.Range(.Cells(a, 1), .Cells(a, 10)).Copy
这些要点非常重要,因为它们会引用当前Cells
,并在Sheet
行中设置With Sheets("ATD")
对象。