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

DonAndress

Active Member
Joined
Sep 25, 2011
Messages
365
Office Version
  1. 365
  2. 2021
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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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,223,967
Messages
6,175,667
Members
452,666
Latest member
AllexDee

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