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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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