Cells method on another worksheet

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,356
lngCol = 5, lngEndCol = 9

the line of code
Sheets("strCols").Range(Cells(2, lngCol), Cells(2, lngEndCol)).Copy
fails with a Run-time error 1003 Application-defined or object-defined error

the alternative
Sheets("strCols").Range("E2:I2").Copy
works fine.

If Sheet("strCols") is active at the time the first line of code is read, there is no problem, but I do not want to activate the sheet just for this line of code.

Does anyone have an idea what may be happening?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try this...
Code:
Sheets("strCols").Range([COLOR="Red"]Sheets("strCols").[/COLOR]Cells(2, lngCol), [COLOR="Red"]Sheets("strCols").[/COLOR]Cells(2, lngEndCol)).Copy

Or this...
Code:
With Sheets("strCols")
.Range([COLOR="Red"].[/COLOR]Cells(2, lngCol), [COLOR="Red"].[/COLOR]Cells(2, lngEndCol)).Copy
End With
 
Upvote 0
Beautiful solution AlphaFrog.

I definitely prefer number two rather than using three references to the worksheet.

This seems to be a limitation of the cells method which is not shared by "Range".

Thanks again for leaping to my rescue.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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