Hi guys,
I've been trying to copy values from one range to another, and the ranges need to be dynamic. Normally I use a combination of the 'range' and 'cells' properties, like this:
or something similar, and it works fine.Code:range(cells(1,1),cells(x,y)).value = range(cells(2,3),cells(a,b).value
However, when I try to do this across two workbooks I get a runtime error. I've isolated the piece of code causing the problem as here:
The thing that confuses me is that if I change the range references to a1 style then it works fine:Code:Sub test() Dim count As Integer count = 15 Workbooks("Output Library").Sheets("Standard Sheet (2)").Range(Cells(1, 1), Cells(count, 3)).Value = _ Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Range(Cells(1, 1), Cells(count, 3)).Value End Sub
Anyone have any ideas why the second sub works but the first doesn't?Code:Sub test() Dim count As Integer count = 15 Workbooks("Output Library").Sheets("Standard Sheet (2)").Range("A1").Value = _ Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Range("A1").Value End Sub
Thanks, Sam.
p.s. I realise I could use copy and paste for this, was just wondering why this method didn't work in this instance.
Last edited by sam0287; 02-22-2010 at 05:35 AM.
Hi Sam
In the first, you need to fully qualify the Cells objects (ie on which sheets/workbooks they appear) as otherwisse by default (in a standard module) they refer to the activesheet (so you have a range on one sheet, but then try to apply cells on another = mismatch):
Note the addition of the periods before th Cells on the left hand side of the statement, and the "Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim")." before the Cells on the right hand side - both are fully qualifying the Cells property.Code:With Workbooks("Output Library").Sheets("Standard Sheet (2)") .Range(.Cells(1, 1), .Cells(count, 3)).Value = _ Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Range(Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Cells(1, 1), Workbooks("Excel Front End v1.2").Sheets("Inputs to Vensim").Cells(count, 3)).Value End With
Richard
Richard Schollar
Microsoft MVP - Excel
Brilliant, thanks a lot!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks