.Range(Cells(row,column)).Select runtime error

Barkingdoggy

New Member
Joined
Jun 21, 2011
Messages
5
Windows 7, Excel 2007 workbook with multiple sheets. VBA script is throwing a Runtime Error 1004, "Application-defined or object-defined error." The line in question is:
Code:
Sheets("Lunches").Range(Cells(8, 26)).Select

Using an "equivalent" statement, however, works fine.
Code:
Sheets("Lunches").Range("z8").Select
What am I missing? I want to select cells by row and column variables in this script.

Thanks!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,

Welcome to the board.

Try:

Code:
Sheets("Lunches").Cells(8,26).Select
Try to avoid .Select at all costs. Instead try something like:

Code:
Sheets("Lunches").Cells(8,26).Copy
Instead of:

Code:
Sheets("Lunches").Cells(8,26).Select
Selection.Copy
 
Upvote 0
Welcome to the board..

It is not necessary to enclose Cells within Range..
Unless trying to select multiple cells...

Try
Sheets("Lunches").Cells(8, 26).Select


Also, it is almost never necessary to select a range to manipulate it.
And further, this line will only work if the Sheet Lunches is ALREADY currently active.
Otherwise it will error. The sheet must be active first before you can select a cell on that sheet.
So it is essentially useless to include the sheetname on a range(or cells) when selecting it.
So simply

Cells(8, 26).Select will sufice.


Hope that helps.
 
Upvote 0
Dropping Range works, but, this is the statement I want:
Code:
Sheets("Lunches").Range(Cells(8, iColumn), Cells(iEndRow, iColumn)).Copy
It gives me the runtime error 1004. I was trying to debug it with the select statements above (which gave the same error). iColumn is set properly, e.g 26 and iEndRow is also set proplerly at e.g 165.
 
Upvote 0
This is where this comes into play

It is not necessary to enclose Cells within Range..
Unless trying to select multiple cells...

When you specify the sheet on the Range, you MUST also specify the sheet on BOTH Cells..

like so..

Sheets("Lunches").Range(Sheets("Lunches").Cells(8, iColumn), Sheets("Lunches").Cells(iEndRow, iColumn)).Copy

To make it easier, so you're not writing the same sheetname over and over...
Use WITH

Code:
With Sheets("Lunches")
    .Range(.Cells(8, iColumn), .Cells(iEndRow, iColumn)).Copy
End With

Notice the period preceding Range and Cells..
That tells it to use the object referenced in the With.


Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top