Arrays VBA错误';方法范围。。。失败';(通过变量声明的范围)

Arrays VBA错误';方法范围。。。失败';(通过变量声明的范围),arrays,vba,excel,Arrays,Vba,Excel,我正在查找两张图纸上的最后一行和最后一列,并将这些值赋给变量。然后,我使用这些变量来形成一个范围的边界,该范围被放入VBA数组中 为什么当我被放置在引用中使用的工作表以外的工作表上时,会出现错误(“对象的“方法”“范围”“失败”)?我用的是代号 代码如下: Private arrPlan() As Variant Private lastRowSource As Long Private lastColSource As Long Private arrRawData() As Variant

我正在查找两张图纸上的最后一行和最后一列,并将这些值赋给变量。然后,我使用这些变量来形成一个范围的边界,该范围被放入VBA数组中

为什么当我被放置在引用中使用的工作表以外的工作表上时,会出现错误(“对象的“方法”“范围”“失败”)?我用的是代号

代码如下:

Private arrPlan() As Variant
Private lastRowSource As Long
Private lastColSource As Long

Private arrRawData() As Variant
Private lastRowDestination As Long
Private lastColDestination As Long

Private arrStrings() As Variant
Private str As String

Public Sub Google_Plan_Into_RawData()


'------------------------ Read Excel ranges into Arrays -----------------

lastRowSource = Sheet1.Range("A" & Rows.count).End(xlUp).Row
lastColSource = Sheet1.Range("A1").End(xlToRight).Column
arrPlan = Sheet1.Range(Cells(1, 1), Cells(lastRowSource, lastColSource))


lastColDestination = Sheet2.Range("A1").End(xlToRight).Column
lastRowDestination = Sheet2.Range("A" & Rows.count).End(xlUp).Row
arrRawData = Sheet2.Range(Cells(1, 1), Cells(lastRowDestination, lastColDestination))
// ...Rest of the code
此行失败:
arrPlan=Sheet1.范围(单元格(1,1),单元格(lastRowSource,lastColSource))
如果我在Sheet2上,当然,它在这一行失败:
arrRawData=Sheet2.范围(单元格(1,1),单元格(lastRowDestination,lastColDestination))

如果我在第一张纸上

我通常做的是:

With Sheet1
lastRowSource = .Range("A" &Rows.count).End(xlUp).Row
lastColSource= .Range("A1").End(xlToRight).Column
arrPlan = .Range(Cells(1, 1), Cells(lastRowSource, lastColSource))
End With. 

还是没有舔。我做错了什么?

尝试使用完整的范围参考,包括工作表代码:

arrPlan = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(lastRowSource, lastColSource))

arrRawData = Sheet2.Range(Sheet2.Cells(1, 1), Sheet2.Cells(lastRowDestination, lastColDestination))

尝试使用完整的范围参考,包括工作表代号:

arrPlan = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(lastRowSource, lastColSource))

arrRawData = Sheet2.Range(Sheet2.Cells(1, 1), Sheet2.Cells(lastRowDestination, lastColDestination))

尝试将工作簿和工作表的引用放置在以下范围内:

arrPlan = Range(Sheet1.Cells(1, 1), Sheet1.Cells(lastRowSource, lastColSource)) if 
第2页:

arrRawData = Range(Sheet2.Cells(1, 1), Sheet2.Cells(lastRowDestination, lastColDestination))

尝试将工作簿和工作表的引用放置在以下范围内:

arrPlan = Range(Sheet1.Cells(1, 1), Sheet1.Cells(lastRowSource, lastColSource)) if 
第2页:

arrRawData = Range(Sheet2.Cells(1, 1), Sheet2.Cells(lastRowDestination, lastColDestination))

隐马尔可夫模型。。。我试过类似的方法,但通常,我会在语句中这样做。这样我就不用担心这种事了,不是吗?但我还是犯了一个错误。(第一个是‘使用Sheet1…’,另一个是‘使用Sheet2…’如果你用语句做这件事,那么在你的问题的代码中显示它!我的意思是——在另一个类似的文档中,我用语句做这件事。我在这里尝试了,同样的问题发生了。因此,我放弃了,并在我的问题中这样做了。奇怪的是,它起作用了ng对我来说很好,每行重复引用工作表三次。正如我在回答中所述,它对我有效,但在使用你问题中的代码时不起作用。嗯……我尝试过类似的方法,但通常我在with语句中这样做。这样我就不必担心这种事情了,不是吗?但我仍然得到了一个er罗。(第一个是‘使用Sheet1…’,另一个是‘使用Sheet2…’如果你用语句做这件事,那么在你的问题的代码中显示它!我的意思是——在另一个类似的文档中,我用语句做这件事。我在这里尝试了,同样的问题发生了。因此,我放弃了,并在我的问题中这样做了。奇怪的是,它起作用了ng对我来说很好,每行重复引用工作表三次。正如我在回答中所述,它对我有效,并且在使用问题中的代码时不起作用。