Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50235

Pasting values between ranges in different workbooks: "Range" works but "Cells" doesn't

$
0
0
Hello! I'm pasting ranges from one workbook to another without actually activating either.
For the "range1" variable I'm using "Range()", and for the "range2" variable I'm using "Cells()".

I'd like to use Cells() for both ranges (easier to loop through numbers than through characters, AFAIK), but I can't use Cells() for the range1. Why is this?

Thanks!

Option 1: Range() for range1 and Cells() for range2 -> works!
Code:

Dim range1, range2 As Range

Set range1 = Workbooks("Arquetipos.xlsx").Worksheets("Minas").Range("A1:A2")
Set range2 = Workbooks("macros v2.xlsm").Sheets("para Claudia").Range(Cells(15, 1), Cells(16, 1))
   
range2.Value = range1.Value

Option 2: Cells() for range1 and Cells() for range2 -> Run-time error '1004': Application-defined or object-defined error
Code:

Dim range1, range2 As Range

Set range1 = Workbooks("Arquetipos.xlsx").Worksheets("Minas").Range(Cells(1, 1), Cells(2, 1))
Set range2 = Workbooks("macros v2.xlsm").Sheets("para Claudia").Range(Cells(15, 1), Cells(16, 1))
   
range2.Value = range1.Value


Viewing all articles
Browse latest Browse all 50235

Trending Articles