Cells property question Run-time error 1004

dpaul2

New Member
Joined
Sep 15, 2019
Messages
2
Hi

Trying to understand Cells property better. When I use Cells property below for Sheet1 it works, but when I try to then use Cells property against SHEET2 I get Runtime error 1004.
It seems that Cells property is tied to Sheet1 somehow. Can someone explain what is happening under the covers. Thanks!!!

Dim varSheetA As Variant
Dim varSheetB As Variant
varSheetA = Worksheets("Sheet1").Range(Cells(1, 1), Cells(16, 7)) - works no issue
varSheetB = Worksheets("Sheet2").Range(Cells(1, 1), Cells(16, 7)) -Run-time error 1004


varSheetB = Worksheets("Sheet2").Range("A1:G16") - works no issue



 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You have not qualified the cells, so they are looking at the active sheet.
It needs to be like
Code:
varSheetB = Worksheets("Sheet2").Range(Worksheets("Sheet2").Cells(1, 1), Worksheets("Sheet2").Cells(16, 7))
Or simpler
Code:
With Worksheets("Sheet2")
   varSheetB = .Range(.Cells(1, 1), .Cells(16, 7))
End With
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,145
Members
452,615
Latest member
bogeys2birdies

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