Professional Documents
Culture Documents
Home
Why am I having issues assigning a Range to an Array of Variants Ask Question
PUBLIC Asked 7 years, 2 months ago Active 4 years ago Viewed 24k times
Questions
I am having a few problems with some VERY simple lines of code. Let me detail the facts and see
Tags The Overflow Blog
if anyone else can replicate this behavior. If any one can replicate I would like to get an explanation
Users 26 of why it is happening. Introducing The Key
and using Sheets in place of Worksheets also all give the same error. Related
I have made sure it is the same sheet as the active referenced sheet by using 1906 Why is using “for…in” for array iteration a
bad idea?
Range("A1:A10").Worksheet.Name Following the working code and it indeed says Sheet1 in the
output. 0 Guidance required for VBA code to copy
data from multiple worksheets to another
worksheet
No other workbooks are open so it can't be referencing another workbook either.
0 Populating an array with named range on a
Now this last bit of code only adds to my confusion as it totally works! specific worksheet
So using the SAME RANGE defined the same way on the same sheet now works when I assign it 0 Cross referencing a cell with sheet number
to a Range Variable. and use that! And as expected this works with both the WorkSheets and
Sheets function regardless of how I define the sheet (I can use the index or the Name of the Hot Network Questions
worksheet and all work fine)
Taking over another fired developers role
If it helps anyone, I am testing this with Excel 2007 on a Windows XP machine. I have not yet How do the functions Reap and Sow actually work
behind the scenes?
tested it on any other machines but I plan to test on 2003 and 2010 on Windows 7 and 8, just
haven't had the chance yet. Missed apple pruning window
1 @user2140261, changing Dim arr() As Variant to Dim arr As Variant fixes problem for me
and arr = Worksheets(1).Range("A1:A10") works fine! – Dmitry Pavliv Jan 27 '14 at 17:14
2 @simoco is correct. Removing () and just leaving Dim arr As Variant will correct the issue on
the Worksheets... part. Now, how to understand all of this. :D – NullDev Jan 27 '14 at 17:17
1 @simoco Yeah dimming it in that manner also fixes it for me, for the record the article I was speaking of
was Here – user2140261 Jan 27 '14 at 17:17
No it is not a bug.
32 The point is that Value is the default property of the Range Object, so why isn't it implicitly
used? Did you have a look at the question I linked? (FROM CHAT)
The experts posting previous answers have already explained very well in details. I will keep the
explanation to minimal and hence let me know if you still have any questions.
Let's understand our objects first. I created this small table which clearly shows what are we
handling so that there is no confusion.
You could also add a Watch to see the Type for a particular object as shown in the pic below.
arr = Range("A1:A10")
Excel knows that the default property is .Value . However in other case, it doesn't know because
Excel is not a mind reader or let's say intelligent enough to understand whether you want to use
Worksheets("Sheet1").Range("A1:A10") as a Range or a Variant
Once you explicitly specify your object as a Range then Excel knows what you want. For example
this works.
Share Edit Follow edited Jan 18 at 12:34 answered Jan 28 '14 at 16:56
Community ♦ Siddharth Rout
1 1 136k 15 189 235
1 +1 for the Watch to show the Type, I had been using TypeName and VarType, and both returned Range
for every item. – user2140261 Jan 28 '14 at 17:09
1 Hope that clarifies when .Value is the default property and when it is not :) – Siddharth Rout Jan 28 '14
at 17:11
plus one for because Excel is not a mind reader or let's say intelligent enough .
this is the most important fact. – L42 Jan 29 '14 at 0:38
Add a comment
Dim arr() as Variant '~~> you declare arr() as array which may contain Varying `Data Type`
Above works because you are not trying to assign Collections of Objects into an array.
Instead, you are assigning a specific entity or value.
Range is an object but not a Collection of Objects .
No.1 example is direct without accessing Sheets Collection Object .
Same is true with
No.2 since you work with Sheet1 which is a Sheet Object but not Collection of Sheet
Objects .
No.3 is self explanatory, you assign .Value to an arr array.
No.4 works because rng is already a Range object by Set which again is not a Collection of
Objects .
So this:
arr = Sheets("Sheet1").Range("A1:A10")
doesn't work because Excel will read this as trying to assign Object from Sheets Collection of
Objects and thus error occurs.
I hope this makes sense a bit.
I think @Siddharth Rout already clear this in his post. Sorry i was sloppy in the way i explain things. :) But
i'm glad somehow we cleared our confusion. – L42 Jan 29 '14 at 0:41
Add a comment
I would say an Array of Something is not the same as a Something, since this Something can
be an Array of some other things. If you define something as an Array, what you assign to it has
2 to be an Array, be it an Array of Number, Text, Range, Chart objects, etc.
When things work that we don't expect, I believe it's the built-in data type conversion that makes
things easy for us most of the time. This conversion may have to be a direct object, not properties
of an object.
For example, Rows and Cols are of type Long, but you can throw the Byte/Double type at it:
You don't have to convert 1.5 to Long; Excel does it all in background for you.
When you define an array of something and assign things to it, Excel does type matching behind
the scene and set values when possible.
?typename(Range("A1:A10")) gives you Range . But when you assign it to arr where Dim
arr() As Variant , Excel converts the Range to an Array using the values of that Range.
However, Excel seems to fail converting if it doesn't have direct access to the object, unless you
created memory for it. For example:
The above code is all fine, but it cannot convert and assign arr =
ThisWorkbook.Worksheets("Sheet1").Range("A1:A10") in one go, unless you throw an array to
it ( ThisWorkbook.Worksheets("Sheet1").Range("A1:A10").Value is of type Variant()).
Share Edit Follow edited Mar 28 '17 at 13:42 answered Jan 28 '14 at 6:27
TylerH PatricK
18.9k 48 65 85 6,177 1 16 24
Plus one. Again it boils down to @Tim Williams statement that declaring a variable as variant is different
from declaring an array variable of variant type. – L42 Jan 28 '14 at 6:42
1 THIS IS FALSE! I don't get why you can't understand that. Because ?
typename(Worksheets("Sheet1").Range("A1:A10")) ALSO gives you Range yet it does NOT
work with Dim arr() As Variant , it's kidn of annoying how many people can take information with
no actual proof of facts to back up the statement, besides saying it is what it is. Then use a statement
thinking it is a factual statement. – user2140261 Jan 28 '14 at 12:36
Add a comment
Your Answer
Links Images Styling/Headers Lists Blockquotes Code HTML Tables Advanced help
Not the answer you're looking for? Browse other questions tagged arrays vba excel or ask your
own question.
STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn Instagram
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Advertising Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Terms of Service
Disable Responsiveness Contact Us
Cookie Settings
site design / logo © 2021 Stack Exchange Inc; user contributions
Cookie Policy licensed under cc by-sa. rev 2021.4.1.38970