“运行时错误”438':对象不支持此属性或方法。 Range.values = Range.values [英] "Run-Time error '438': Object doesn't support this property or method." Range.values = Range.values

查看:474
本文介绍了“运行时错误”438':对象不支持此属性或方法。 Range.values = Range.values的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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


Windows Source)。激活 - Sheet(SourceSheet)选择 -
范围(SourceRange)复制 - Windows(DestinationWorksheet)激活
- 范围(DestinationRange)。粘贴



DestinationWorkBook.DestinationSheet.Range(DestinationRange)。Value =
SourceWorkBook.SourceWorkSheet.Range(SourceRange)。值


我使用工作表点击名称和字母范围进行工作:

 工作簿(DestinationWorkBook ).Sheets(DestinationSheet)。Range(A:C)。Value = _ 
工作簿(SoureceWorkBook).Sheets(SourceSheet)。Range(A:C)。 b

还使用半动态范围和工作表点击名称:

  lastRow = Cells(工作簿( Limits_Name).Sheets(SourceSheet)。Rows.Count,_ 
A)。End(xlUp).Row
工作簿(DestinationWorkBook).Sets(DestinationSheet)。Range :C& lastRow).Value = _
工作簿(SourceWorkBook).Sheets(SourceSheet)。Range(A1:C& lastRow).Value

当我使用工作表对象名称而不是工作表名称或单元格而不是范围时,我的问题开始。在这种情况下,当我收到这个错误:

 工作簿(DestinationWorkBook).shtDestinationSheet.Range(A:C)。 Value = _ 
工作簿(SourceWorkBook).Sheets(SourceSheet)。Range(A:C)。value

OR

lastRow = Cells工作簿(SourceWorkBook).Sheets(SourceSheet)。Rows.Count,A)。End(xlUp).Row
lastCol = Cells(1,Workbooks(SourceWorkBook).Sheets(SourceSheet)。 .Count).End(xlToLeft).Column
工作簿(DestinationWorkBook).Sheets(DestinationSheet)。范围(单元格(1,1),单元格(lastRow,lastCol))Value = _
工作簿(SourceWorkBook).Sheets(SourceSheet)。范围(单元格(1,1),单元格(lastRow,lastCol))值

OR(这是理想代码)

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

工作簿(DestinationWorkBook).shtDestinationSheet.Range(C (1,1),Cells(lastRow,lastCol))Value = _
工作簿(SourceWorkBook).Sheets(SourceSheet)。我想知道使用表格(sheetname)和...之间的区别是什么?以及可以在工作表对象属性的(name)属性下给出的工作表对象名称。



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



我喜欢使用工作表对象名称,因为VBA代码仍然可以

解决方案

第一个问题(已解决):

当使用对象工作表这还包括工作簿
虽然 Worksheet-Object 不是像 Workbook.Worksheet_Object 。所以要么使用 Workbook.Worksheet(Worksheet_Object.Name)或只是 Worksheet_Object



第二个问题(已解决):

使用 Range(Cells(),Cells())活动的工作簿...仅使用 Cells()没有父进程有时会导致VBA想要使用完整路径的问题。只要单元格将重新使用[工作簿] Sheet!范围,同时使用不同的父代码会导致错误。 VBA将得到如下回报: Wb1.Ws1.Range(Wb2.Ws2.Range)



你可以尝试一下:

  htDestinationSheet.Range(htDestinationSheet.Cells(1,1),htDestinationSheet.Cells(lastRow,lastCol)) .Value = Workbooks(SourceWorkBook).Sheets(SourceSheet)。范围(工作簿(SourceWorkBook).Sheets(SourceSheet)。Cells(1,1),Workbooks(SourceWorkBook).Sheets(SourceSheet)。Cells lastRow,lastCol))价值

哪些应该工作...但是:我觉得最好留下与 str (看起来更好)


I am trying to copy a range of data from a Excel workbook to another workbook without the need of selecting any workbook during this process and using worksheet object names.

I want to do this because the selection process:

Windows("SourceWorksheet").Activate - Sheet("SourceSheet").Select - Range("SourceRange").Copy - Windows("DestinationWorksheet").Activate - Sheet("DestinationSheet").Select - Range("DestinationRange").Paste

is very slow compare with

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

I have got this working using sheets tap names and letter ranges:

Workbooks(DestinationWorkBook).Sheets("DestinationSheet").Range("A:C").Value = _
       Workbooks(SoureceWorkBook).Sheets("SourceSheet").Range("A:C").Value

And also using semi-dynamic ranges and sheets tap names:

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

My problems starts when I use sheets object names instead of sheets names or cells instead of ranges. In those situation is when I get that error:

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

I would like to know what is the difference between using Sheets("sheetname") and the and the sheet object name which can be given under the (name) property of the worksheet object properties.

If I use Sheets("SourceSheet").Range("") I do not need to select the sheet but using sthSourceSheet.Range("") I do.

I like to use sheet object names because the VBA code still works if the sheet name is modified.

解决方案

First problem (solved):
When using an object for a Worksheet this includes also the Workbook. While the Worksheet-Object is not a child of the workbook itself inside of the syntax like Workbook.Worksheet_Object. So either use Workbook.Worksheet(Worksheet_Object.Name) or just Worksheet_Object

Second probem (solved):
There is a problem using Range(Cells(), Cells()) in a non-active workbook... Using only Cells() with no parent sometimes causes trouble cus VBA want's to use a full path. Just Cells will retun a [workbook]Sheet!Range while using with a different parent this causes an error. VBA will get a return like: Wb1.Ws1.Range(Wb2.Ws2.Range).

You can try something like:

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

which should work... However: i think it's better to stay with str (looks better)

这篇关于“运行时错误”438':对象不支持此属性或方法。 Range.values = Range.values的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆