Code to copy range between workbooks

Jlascu

New Member
Joined
Mar 5, 2018
Messages
11
Hi ... hope anyone can help me. I'm trying to copy a range from one workbook (x.Sheets("Pf")) to another (y.Sheets("ER")).
Using 2 different methods and none work! any pearls of wisdom? greatly appreciate any help!

Thanks!

jlascu

--

Method 1 - Setting Ranges
Set src2Range = Range(Cells(Worksheets("Ctrls").Range("C24").Value + 5, 1), Cells(iNoRecords + 5, Worksheets("Ctrls").Range("C20")))
Set dest2Range = Range(Cells(5, Worksheets("Ctrls").Range("D21").Value), Cells(5, iNoRecords))
y.Sheets("Ctrls").Range(dest2Range.Address).Value = x.Sheets("Pf").src2Range.Value

Error: Run time error 438: Doesn't Support This Property or Method

Method 2 - Direct
y.Sheets("ER").Range(Cells(5, Worksheets("Ctrls").Range("D21").Value), Cells(5, iNoRecords)).Value = _
x.Sheets("Pf").Range(Cells(Worksheets("Ctrls").Range("C24").Value + 5, 1), _
Cells(iNoRecords + 5, Worksheets("Ctrls").Range("C20").Value)).Value

Error: Run time error 1004: Application-defined or object-defined error
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
For method one, try.
Code:
dest2Range = src2Range.Value
in place of
y.Sheets("Ctrls").Range(dest2Range.Address).Value = x.Sheets("Pf").src2Range.Value
 
Upvote 0
many thanks!! It cleared the error!
however, code just runs thru without pasting data. could there be a step missing in the code to paste?
many thanks again for the help!!
 
Upvote 0
I don't know. Without seeing the worksheet(s) or know where the data resides, it is difficult to analyze that type of problem from the paucity of code provided. My suggestion was to simply use the variables that you set for your source and destination ranges more efficiently than they were being used. If you are not getting values associated with those ranges, perhaps you are using the wrong parameters. To check that, you can use a message box in the code to see if the correct range address is created for your variables. Insert the following after your Set statements.

Code:
MsgBox src2Range.Address & vbLf & dest2Range.Address

Then check those ranges to make sure they are what you expected. Also make sure the range is on the right sheet.
 
Last edited:
Upvote 0
Your Set statements as written would put you source range and your destination range on the same worksheet because the Range is not qualified with a parent. Like this:
Code:
Set src2Range = x.Sheets("pf").Range(Cells(Worksheets("Ctrls").Range("C24").Value + 5, 1), Cells(iNoRecords + 5, Worksheets("Ctrls").Range("C20")))
Set dest2Range = ySheets("ER").Range(Cells(5, Worksheets("Ctrls").Range("D21").Value), Cells(5, iNoRecords))
destRange = srcRange.Value
 
Upvote 0
Hi Again,

Seems the error is on the qualifying, below is the entire code.
Many thanks for the help, really appreciate you taking the time!

JL

Code:
[Dim x As Workbook
Dim y As Workbook
Dim src2Range, dest2Range As Range

'Set both workbooks
Set x = Workbooks(sSPVFileName): Set y = Workbooks(sDPMPName) ‘x = data source; y = data

'Pass # records to DPMP
'For d = 1 To Worksheets("Ctrls").Range("C23").Value

Set src2Range = x.Sheets("Pf").Range(Cells(Worksheets("Ctrls").Range("C24").Value + 5, 1), Cells(iNoRecords + 5, Worksheets("Ctrls").Range("C20")))
Set dest2Range = y.Sheets("ER").Range(Cells(5, Worksheets("Ctrls").Range("D21").Value), Cells(5, iNoRecords))
MsgBox src2Range.Address & vbLf & dest2Range.Address

destRange = srcRange.Value

' y.Sheets("ER").Range(Cells(5, Worksheets("Ctrls").Range("D21").Value), Cells(5, iNoRecords)).Value = _
x.Sheets("Pf").Range(Cells(Worksheets("Ctrls").Range("C24").Value + 5, 1), _
Cells(iNoRecords + 5, Worksheets("Ctrls").Range("C20").Value)).Value -à Tried it didn’t work

Next]
 
Upvote 0
Happy to help,
regards, JLG
 
Upvote 0
Solution

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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