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



 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,127,632
Messages
5,625,988
Members
416,149
Latest member
Bigpotato 668

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
Top