我在两张纸上找到最后一行和最后一列,并将这些值分配给变量。然后,我使用这些变量来形成一个范围的边界,该范围被放入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.
仍然没有舔。我在这里做错了什么?
答案 0 :(得分:2)
尝试使用完整范围参考,包括工作表代号:
arrPlan = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(lastRowSource, lastColSource))
arrRawData = Sheet2.Range(Sheet2.Cells(1, 1), Sheet2.Cells(lastRowDestination, lastColDestination))
答案 1 :(得分:0)
尝试将工作簿和工作表的引用放在范围内:
arrPlan = Range(Sheet1.Cells(1, 1), Sheet1.Cells(lastRowSource, lastColSource)) if
表2:
arrRawData = Range(Sheet2.Cells(1, 1), Sheet2.Cells(lastRowDestination, lastColDestination))