Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba &引用;运行时错误';438';:对象没有';不支持此属性或方法。”;Range.values=Range.values_Vba_Excel_Range - Fatal编程技术网

Vba &引用;运行时错误';438';:对象没有';不支持此属性或方法。”;Range.values=Range.values

Vba &引用;运行时错误';438';:对象没有';不支持此属性或方法。”;Range.values=Range.values,vba,excel,range,Vba,Excel,Range,我正在尝试将一系列数据从Excel工作簿复制到另一个工作簿,而无需在此过程中选择任何工作簿并使用工作表对象名称 我想这样做是因为选择过程: Windows(“源工作表”)。激活-工作表(“源工作表”)。选择- 范围(“源范围”)。复制-窗口(“目标工作表”)。激活 -工作表(“目标工作表”)。选择-范围(“目标范围”)。粘贴 与之相比是非常慢的 DestinationWorkBook.DestinationSheet.Range(“DestinationRange”).Value= Source

我正在尝试将一系列数据从Excel工作簿复制到另一个工作簿,而无需在此过程中选择任何工作簿并使用工作表对象名称

我想这样做是因为选择过程:

Windows(“源工作表”)。激活-工作表(“源工作表”)。选择- 范围(“源范围”)。复制-窗口(“目标工作表”)。激活 -工作表(“目标工作表”)。选择-范围(“目标范围”)。粘贴

与之相比是非常慢的

DestinationWorkBook.DestinationSheet.Range(“DestinationRange”).Value= SourceWorkBook.SourceWorkSheet.Range(“SourceRange”).Value

我已使用工作表、点击名称和字母范围完成此工作:

Workbooks(DestinationWorkBook).Sheets("DestinationSheet").Range("A:C").Value = _
       Workbooks(SoureceWorkBook).Sheets("SourceSheet").Range("A:C").Value
并使用半动态范围和图纸点击名称:

lastRow = Cells(Workbooks(Limits_Name).Sheets("SourceSheet").Rows.Count, _
          "A").End(xlUp).Row
Workbooks(DestinationWorkBook).Sheets("DestinationSheet").Range("A1:C" & lastRow).Value = _
          Workbooks(SourceWorkBook).Sheets("SourceSheet").Range("A1:C" & lastRow).Value
当我使用图纸对象名称而不是图纸名称或单元格而不是范围时,问题就开始了。在这种情况下,当我出现错误时:

Workbooks(DestinationWorkBook).shtDestinationSheet.Range("A:C").Value = _
        Workbooks(SourceWorkBook).Sheets("SourceSheet").Range("A:C").Value 

OR

lastRow = Cells(Workbooks(SourceWorkBook).Sheets("SourceSheet").Rows.Count, "A").End(xlUp).Row
lastCol = Cells(1, Workbooks(SourceWorkBook).Sheets("SourceSheet").Columns.Count).End(xlToLeft).Column
Workbooks(DestinationWorkBook).Sheets("DestinationSheet").Range(Cells(1, 1), Cells(lastRow, lastCol)).Value = _
   Workbooks(SourceWorkBook).Sheets("SourceSheet").Range(Cells(1, 1), Cells(lastRow, lastCol)).Value

OR (this is the ideal code)

lastRow = Cells(Workbooks(SourceWorkBook).Sheets("SourceSheet").Rows.Count, "A").End(xlUp).Row
lastCol = Cells(1, Workbooks(SourceWorkBook).Sheets("SourceSheet").Columns.Count).End(xlToLeft).Column

Workbooks(DestinationWorkBook).shtDestinationSheet.Range(Cells(1, 1), Cells(lastRow, lastCol)).Value = _
   Workbooks(SourceWorkBook).Sheets("SourceSheet").Range(Cells(1, 1), Cells(lastRow, lastCol)).Value
我想知道使用工作表(“sheetname”)与工作表对象属性的(name)属性下提供的和工作表对象名称之间的区别

如果我使用Sheets(“SourceSheet”).Range(“”),我不需要选择工作表,但需要使用sthSourceSheet.Range(“”)

我喜欢使用图纸对象名称,因为如果修改图纸名称,VBA代码仍然有效。

第一个问题(已解决):
将对象用于
工作表时
还包括
工作簿
。 而
工作表对象
不是工作簿本身在语法中的子对象,如
工作簿.Worksheet\u对象
。因此,要么使用
Workbook.Worksheet(工作表\u对象.Name)
,要么只使用
Worksheet\u对象

第二个问题(已解决):
在非活动工作簿中使用范围(单元格(),单元格())时出现问题。。。在没有父级的情况下仅使用
单元格()
有时会导致VBA希望使用完整路径的问题。只需
单元格
即可重新运行[工作簿]工作表!与其他父级一起使用时,这会导致错误。VBA将得到如下返回:
Wb1.Ws1.Range(Wb2.Ws2.Range)

您可以尝试以下方法:

htDestinationSheet.Range(htDestinationSheet.Cells(1, 1), htDestinationSheet.Cells(lastRow, lastCol)).Value = Workbooks(SourceWorkBook).Sheets("SourceSheet").Range(Workbooks(SourceWorkBook).Sheets("SourceSheet").Cells(1, 1), Workbooks(SourceWorkBook).Sheets("SourceSheet").Cells(lastRow, lastCol)).Value

这应该是有效的。。。但是:我认为最好还是使用
str
(看起来更好)

也许我弄错了,但是当设置工作表对象时,父对象(工作簿)被包含在内。。。拥有
workbook\u object.sheet\u object
通常会导致错误…感谢@DirkReichel的评论,你是对的,解决了部分问题。现在,此代码正在工作:
shtdestinitionsheet.Range(“A:C”).Value=工作簿(source工作簿).Sheets(“SourceSheet”).Range(“A:C”).Value
和半动态范围也在工作:
shtdestinitionsheet.Range(“A1:C”&lastRow).Value=工作簿(soure工作簿).Sheets(“SourceSheet”)。(“A1:C”&lastRow).Value
。但是我仍然不能使用cells()使用完整的动态范围:
.Range(cells(1,1),cells(lastRow,lastCol))
通常应该没有问题。。。你犯了什么错误?如果您为
Range()
使用字符串,比如
str=Range(单元格(1,1),单元格(lastRow,lastCol))。地址
,然后简单地使用
。Range(str)
?感谢@DirkReichel的快速响应,您的解决方案有效:
str=Range(单元格(1,1),单元格(lastRow,lastCol)).Address
shtdestationsheet.Range(Str).Value=工作簿(source工作簿).Sheets(“SourceSheet”).Range(Str).Value
。但是将范围(Str)替换为范围(单元格(1,1),单元格(lastRow,lastCol))我得到了这个错误:运行时错误“1004”应用程序定义的错误或对象定义的错误感谢@DirkReichel,该解决方案工作得很好,由于您的解释,现在我知道了为什么它以前不工作。我完全同意你的看法,解决方案更好。再次非常感谢!