0

I am finding last row and last column on two sheets and assigning these values to variables. Then, I use these variables to form bounds of a range which gets put into a VBA array.

Why am I getting an error ("Method 'Range' of object '_Worksheet' failed") when I'm positioned on a sheet other than the one used in the reference? I am using codenames.

Here's the code:

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

It fails on this line:
arrPlan = Sheet1.Range(Cells(1, 1), Cells(lastRowSource, lastColSource)) if I'm on Sheet2 and of course, it fails on this line:
arrRawData = Sheet2.Range(Cells(1, 1), Cells(lastRowDestination, lastColDestination))
if I'm on Sheet1.

What I usually do is:

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. 

Still no lick. What am I doing wrong here?

2 Answers 2

2

Try to use complete range reference including worksheets codename:

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

arrRawData = Sheet2.Range(Sheet2.Cells(1, 1), Sheet2.Cells(lastRowDestination, lastColDestination))
Sign up to request clarification or add additional context in comments.

6 Comments

Hmm... I tried something similar but normally, I do this inside a With statement. that way I shouldnt have to worry about this kind of thing, no? But I still get an error. (First one is 'With Sheet1...' and the other is 'With Sheet2...'
if you do this with With statement then show it in your code in your question!
What I meant was - in another similar document, I do this with a With statement. I tried here and the same problem happened. So, I discarded With and did like in my question.
strange, it is working fine for me with repeated references to Worksheets three times in each line.
it is working for me as I presented in my answer and is not working when using code from your question.
|
0

Try to place the reference for workbook and worksheet inside the range:

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

Sheet 2:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.