。复制方法失败时工作表未选中

这是我的脚本的一部分,轻松循环复制从一个表到另一个行的一些行:

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

但我还有一个问题。 如果.Copyfunction需要,我想要复制的单元格在当前选定的表单中,还是我在这里丢失了一些东西?

你可以尝试:

 .Range(.Cells(a, 1), .Cells(a, 10)).Copy 

这些点是非常重要的,因为它们With Sheets("ATD")行中的With Sheets("ATD")对象设置当前Cells