"Run-Time error '438': Object doesn't support this property or method." Range.values = Range.values
"Run-Time error '438': Object doesn't support this property or method." Range.values = Range.values
我正在尝试将一系列数据从 Excel 工作簿复制到另一个工作簿,在此过程中不需要 selecting 任何工作簿并使用 worksheet 对象名称.
我想这样做是因为select离子过程:
Windows("SourceWorksheet").Activate - Sheet("SourceSheet").Select -
Range("SourceRange").Copy - Windows("DestinationWorksheet").Activate
- Sheet("DestinationSheet").Select - Range("DestinationRange").Paste
与
相比非常慢
DestinationWorkBook.DestinationSheet.Range("DestinationRange").Value =
SourceWorkBook.SourceWorkSheet.Range("SourceRange").Value
我已经使用 sheets tap 名称和字母范围完成了这项工作:
Workbooks(DestinationWorkBook).Sheets("DestinationSheet").Range("A:C").Value = _
Workbooks(SoureceWorkBook).Sheets("SourceSheet").Range("A:C").Value
并且还使用半动态范围和 sheets 抽头名称:
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
当我使用 sheets 对象名称而不是 sheets 名称或使用单元格而不是范围时,我的问题就开始了。在那种情况下,当我收到该错误时:
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
我想知道使用 Sheets("sheetname") 和 sheet object name 之间有什么区别,可以在 (name) 属性 下给出工作sheet 对象属性。
如果我使用 Sheets("SourceSheet").Range("") 我不需要 select sheet 但使用 sthSourceSheet.Range("") 我做。
我喜欢使用 sheet 对象名称,因为如果修改 sheet 名称,VBA 代码仍然有效。
第一个问题(已解决):
当为 Worksheet
使用对象时,这也包括 Workbook
。
虽然 Worksheet-Object
不是工作簿本身的子项,但在 Workbook.Worksheet_Object
等语法中。所以要么使用 Workbook.Worksheet(Worksheet_Object.Name)
要么只使用 Worksheet_Object
第二个问题(已解决):
在非活动工作簿中使用 Range(Cells(), Cells())
时出现问题...仅使用没有父项的 Cells()
有时会导致麻烦,因为 VBA 想要使用完整路径。 Just Cells
将重新调整 [workbook]Sheet!Range 与不同的父级一起使用时会导致错误。 VBA 会得到一个 return 像: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
(看起来更好)
我正在尝试将一系列数据从 Excel 工作簿复制到另一个工作簿,在此过程中不需要 selecting 任何工作簿并使用 worksheet 对象名称.
我想这样做是因为select离子过程:
Windows("SourceWorksheet").Activate - Sheet("SourceSheet").Select - Range("SourceRange").Copy - Windows("DestinationWorksheet").Activate - Sheet("DestinationSheet").Select - Range("DestinationRange").Paste
与
相比非常慢DestinationWorkBook.DestinationSheet.Range("DestinationRange").Value = SourceWorkBook.SourceWorkSheet.Range("SourceRange").Value
我已经使用 sheets tap 名称和字母范围完成了这项工作:
Workbooks(DestinationWorkBook).Sheets("DestinationSheet").Range("A:C").Value = _
Workbooks(SoureceWorkBook).Sheets("SourceSheet").Range("A:C").Value
并且还使用半动态范围和 sheets 抽头名称:
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
当我使用 sheets 对象名称而不是 sheets 名称或使用单元格而不是范围时,我的问题就开始了。在那种情况下,当我收到该错误时:
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
我想知道使用 Sheets("sheetname") 和 sheet object name 之间有什么区别,可以在 (name) 属性 下给出工作sheet 对象属性。
如果我使用 Sheets("SourceSheet").Range("") 我不需要 select sheet 但使用 sthSourceSheet.Range("") 我做。
我喜欢使用 sheet 对象名称,因为如果修改 sheet 名称,VBA 代码仍然有效。
第一个问题(已解决):
当为 Worksheet
使用对象时,这也包括 Workbook
。
虽然 Worksheet-Object
不是工作簿本身的子项,但在 Workbook.Worksheet_Object
等语法中。所以要么使用 Workbook.Worksheet(Worksheet_Object.Name)
要么只使用 Worksheet_Object
第二个问题(已解决):
在非活动工作簿中使用 Range(Cells(), Cells())
时出现问题...仅使用没有父项的 Cells()
有时会导致麻烦,因为 VBA 想要使用完整路径。 Just Cells
将重新调整 [workbook]Sheet!Range 与不同的父级一起使用时会导致错误。 VBA 会得到一个 return 像: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
(看起来更好)