我在两张表上找到最后一行和最后一列,并将这些值分配给变量。然后,我使用这些变量来形成一个范围的边界,并将其放入VBA数组中。VBA错误'失败的方法范围'(范围通过变量声明)
当我位于参考中使用的纸张以外的纸张上时,为什么会收到错误(“方法范围”对象'_Worksheet'失败“)?我正在使用代号。
下面的代码:
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.Range(Cells(1, 1), Cells(lastRowSource, lastColSource))
如果我在Sheet2上,当然,它不能在这条线:
arrRawData = Sheet2.Range(Cells(1, 1), Cells(lastRowDestination, lastColDestination))
如果我在Sheet1上。
我最常做的是:
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.
仍然没有舔。我在这里做错了什么?
嗯......我尝试了类似的方法,但通常情况下,我在With语句中这样做。这样我不应该担心这种事情,不是吗?但我仍然遇到错误。 (第一个是'With Sheet1 ...',另一个是'With Sheet2 ...' –
如果你用'With statement'做这个,然后在你的代码中显示它! –
我的意思是 - 在另一个类似的文件,我用With语句做了这个,我在这里尝试了,发生了同样的问题,于是我放弃了With,并且在我的问题中做了类似的工作 –