range.copy 目标“A1”与“单元格(1,1)”Excel/VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44805982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
range.copy Destination "A1" vs "Cells(1,1)" Excel/VBA
提问by BuckTurgidson
Consider the below:
考虑以下问题:
Sheets("X").Activate
Sheets("X").Range(Cells(1, 1), Cells(1, 30)).Copy Destination:=Sheets("Y").Range("A1") 'Syntax 1
Sheets("X").Range(Cells(1, 1), Cells(1, 30)).Copy Destination:=Sheets("Y").Range(Cells(1, 1)) 'Syntax 2
Why does Syntax 1 works while Syntax 2 runs into 'Application-defined or object-defined error'?
为什么语法 1 可以工作,而语法 2 遇到“应用程序定义或对象定义的错误”?
采纳答案by A.S.H
Unqualified Cells(1,1)
belongs to the ActiveSheet, which is currently Sheets("X"), so it does not belong to Sheets("Y")
.
UnqualifiedCells(1,1)
属于 ActiveSheet,当前是 Sheets("X"),所以不属于Sheets("Y")
.
OTOH: this should work:
OTOH:这应该有效:
Destination:=Sheets("Y").Range(Sheets("Y").Cells(1, 1), Sheets("Y").Cells(1, 1))
' ^^^^^^^^^^^^^
Dont use unqualified ranges. Drop the Activate
stuf altogether from your code.
不要使用不合格的范围。Activate
从您的代码中完全删除这些东西。
回答by Darren Bartrup-Cook
You haven't qualified the sheet name. So the copy happens on the ActiveSheet it then tries to reference Cell(1,1) from the ActiveSheet but on sheet Y:
您尚未限定工作表名称。所以复制发生在 ActiveSheet 上,然后它尝试从 ActiveSheet 引用 Cell(1,1) 但在工作表 Y 上:
Sheets("Y").Range(Cells(1, 1))
Sheets("Y").Range(Cells(1, 1))
Sheets("Y") is sheet Y. Cells(1,1) is the ActiveSheet.
Sheets("Y") 是工作表 Y。Cells(1,1) 是 ActiveSheet。
The copy only works because you activate sheet X first. Remove that line, select another sheet and it will fail on that as well.
该副本仅在您首先激活工作表 X 时才有效。删除那条线,选择另一张纸,它也会失败。