I run a macro with the following code:
And it works fine, copying the cells from name1 to name2.Code:Dim myArray() As Variant myArray = Worksheets("name1").Range("A1:B1").Value Worksheets("name2").Range("A1:B1").Value = myArray
If I replace the code with this:
I get Run time error 1004: Application defined or object defined errorCode:Dim myarray() As Variant myarray = Worksheets("name1").Range(Cells(1, 1), Cells(1, 2)).Value Worksheets("name2").Range(Cells(1, 1), Cells(1, 2)).Value = myarray
Why?
Many thanks!
Last edited by dudamel; 02-05-2010 at 01:18 AM.
Any unqualified range reference in a code module refers to the active sheet:
Since only one (at most) of those sheets can be active at a time, one of those lines will ALWAYS generate an error.Code:myarray = Worksheets("name1").Range(Cells(1, 1), Cells(1, 2)).Value Worksheets("name2").Range(Cells(1, 1), Cells(1, 2)).Value = myarray
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Thank you!
You can qualify the cells inside the two argument form of Range
Code:myarray = Range(Sheets("Name1").Cells(1,1), Sheet("Name1").Cells(1,2)).Value With Sheets("name2") Range(.Cells(1,1), .Cells(1,2)).Value = myarray End With
_
...How to Cross-post politely...
..Wrap code by selecting the code and clicking the # or read this. Thank you.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks