“运行时错误'438':对象不支持此属性或方法。”Range.values = Range.values

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

我想这样做是因为select过程:

Windows(“SourceWorksheet”)。激活 – 工作表(“SourceSheet”)。select范围(“SourceRange”)。 )。糊

与之相比非常慢

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 

还使用半dynamic范围和工作表的名称:

 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”)和工作表对象属性的(名称)属性下可以给出的工作表对象名称之间的区别是什么。

如果我使用表(“SourceSheet”)。范围(“”)我不需要select工作表,但使用sthSourceSheet.Range(“”)我这样做。

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

第一个问题(解决):
当使用Worksheet的对象时,这也包括Workbook 。 虽然Worksheet-Object不像Workbook.Worksheet_Object这样的语法内部的工作簿本身。 所以要么使用Workbook.Worksheet(Worksheet_Object.Name)要么只使用Worksheet_Object

第二个问题(解决):
在非活动工作簿中使用Range(Cells(), Cells())时出现问题…仅使用没有父项的Cells()有时会导致麻烦,因此VBA希望使用完整path。 Just Cells会重新调用[工作簿] Sheet!Range,而使用不同的父项会导致错误。 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 (看起来更好)