copy paste using Range(Cells,Cells) - error 1004

DonAndress

Active Member
Joined
Sep 25, 2011
Messages
364
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello.

I have two lines of code.
Can anyone please help me understand why first one works and the second one does not?

VBA Code:
Workbooks(Wkb).Sheets(Sht).Range("C12:BF12").Value = Workbooks(Wkb).Sheets(ShtS).Range("C12:BF12").Offset(i, 0).Value

Code:
Workbooks(Wkb).Sheets(Sht).Range(Cells(12, 3), Cells(12, allCols)).Value = Workbooks(Wkb).Sheets(ShtS).Range(Cells(12 + i, 3), Cells(12 + i, allCols)).Value

All variables have values, both lines refer to the same Range (I checked in the Immediate window).
allCols = 58 = BF
The second line is throwing me "Run-time error '1004', Application-defined or object-defined error".
How to fix that to properly use Cells?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
This is a very common question: you need to qualify the Range and Cells properties with the same worksheets:

Code:
Workbooks(Wkb).Sheets(Sht).Range(Workbooks(Wkb).Sheets(Sht).Cells(12, 3), Workbooks(Wkb).Sheets(Sht).Cells(12, allCols)).Value = Workbooks(Wkb).Sheets(ShtS).Range(Workbooks(Wkb).Sheets(ShtS).Cells(12 + i, 3), Workbooks(Wkb).Sheets(ShtS).Cells(12 + i, allCols)).Value

Using Worksheet variables would make the code neater and easier to maintain.
 
Last edited:
Upvote 0
Solution
I would as Rory suggested be using variables for the sheets but I believe you can cut it down to something like this:
VBA Code:
    With Workbooks(Wkb).Sheets(Sht)
        Range(.Cells(12, 3), .Cells(12, allCols)).Value = Range(Workbooks(Wkb).Sheets(ShtS).Cells(12 + i, 3), Workbooks(Wkb).Sheets(ShtS).Cells(12 + i, allCols)).Value
    End With
 
Upvote 0
I would not recommend using unqualified Range calls. If the code is in a worksheet module, it will cause an error.
 
Upvote 0
Rory, Alex.

Thank you so much, it works now.
I also changed the code to use Worksheet variables.

Is it always mandatory to qualify Cells?
 
Upvote 0
Not mandatory (any more than it is with Range), but it's always safer. :)
 
Upvote 0

Forum statistics

Threads
1,221,288
Messages
6,159,038
Members
451,533
Latest member
MCL_Playz

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