PC Review


Reply
Thread Tools Rate Thread

Assigning range to variant

 
 
vezerid
Guest
Posts: n/a
 
      12th Mar 2007
Hi all,

I thought (apparently wrongly), that when we assign a range to a
variant variable then this variable becomes an array. Motivation comes
from this snippet:

Dim v
v = Range("A1:A10")
Range("B1:B10") = v

This effectively copies the values of A1:A10 to B1:B10, something that
cannot be done with
Range("B1:B10") = Range("A1:A10"). Now, the following code:

Option Base 0
Sub test()
Dim v
v = Range("A1:A10")
Debug.Print LBound(v), UBound(v)
For i = 1 To 10
Debug.Print v(i)
Next i
End Sub

1. It does not complain about LBound(v) or UBound(v). Hence, in this
respect, it handles v as an array.
2. Even though Option Base is 0, LBound = 1 and Ubound = 10 (instead
of the 0 and 9 I expected). In this respect it handles it as an array
but does not honor Option Base.
3. v(1) producess the Subscript out of range error. In this respect it
refuses to see it as an array.

So what exactly happens when we assign a range array to a variant?

TIA
Kostis Vezerides

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      12th Mar 2007
RAnge("B1:B10").Value = Range("A1:A10").Value
works fine for me.

if you do

v = Range("A1:A10").Value

then you have to do

for i = 1 to ubound(v)
debug.print v(i,1)
Next

since v is v(1 to 10, 1 to 1)

--
Regards,
Tom Ogilvy


"vezerid" wrote:

> Hi all,
>
> I thought (apparently wrongly), that when we assign a range to a
> variant variable then this variable becomes an array. Motivation comes
> from this snippet:
>
> Dim v
> v = Range("A1:A10")
> Range("B1:B10") = v
>
> This effectively copies the values of A1:A10 to B1:B10, something that
> cannot be done with
> Range("B1:B10") = Range("A1:A10"). Now, the following code:
>
> Option Base 0
> Sub test()
> Dim v
> v = Range("A1:A10")
> Debug.Print LBound(v), UBound(v)
> For i = 1 To 10
> Debug.Print v(i)
> Next i
> End Sub
>
> 1. It does not complain about LBound(v) or UBound(v). Hence, in this
> respect, it handles v as an array.
> 2. Even though Option Base is 0, LBound = 1 and Ubound = 10 (instead
> of the 0 and 9 I expected). In this respect it handles it as an array
> but does not honor Option Base.
> 3. v(1) producess the Subscript out of range error. In this respect it
> refuses to see it as an array.
>
> So what exactly happens when we assign a range array to a variant?
>
> TIA
> Kostis Vezerides
>
>

 
Reply With Quote
 
vezerid
Guest
Posts: n/a
 
      12th Mar 2007
Thank you Tom

Kostis

On Mar 12, 7:39 pm, Tom Ogilvy <TomOgi...@discussions.microsoft.com>
wrote:
> RAnge("B1:B10").Value = Range("A1:A10").Value
> works fine for me.
>
> if you do
>
> v = Range("A1:A10").Value
>
> then you have to do
>
> for i = 1 to ubound(v)
> debug.print v(i,1)
> Next
>
> since v is v(1 to 10, 1 to 1)
>
> --
> Regards,
> Tom Ogilvy
>
> "vezerid" wrote:
> > Hi all,

>
> > I thought (apparently wrongly), that when we assign a range to a
> > variant variable then this variable becomes an array. Motivation comes
> > from this snippet:

>
> > Dim v
> > v = Range("A1:A10")
> > Range("B1:B10") = v

>
> > This effectively copies the values of A1:A10 to B1:B10, something that
> > cannot be done with
> > Range("B1:B10") = Range("A1:A10"). Now, the following code:

>
> > Option Base 0
> > Sub test()
> > Dim v
> > v = Range("A1:A10")
> > Debug.Print LBound(v), UBound(v)
> > For i = 1 To 10
> > Debug.Print v(i)
> > Next i
> > End Sub

>
> > 1. It does not complain about LBound(v) or UBound(v). Hence, in this
> > respect, it handles v as an array.
> > 2. Even though Option Base is 0, LBound = 1 and Ubound = 10 (instead
> > of the 0 and 9 I expected). In this respect it handles it as an array
> > but does not honor Option Base.
> > 3. v(1) producess the Subscript out of range error. In this respect it
> > refuses to see it as an array.

>
> > So what exactly happens when we assign a range array to a variant?

>
> > TIA
> > Kostis Vezerides



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble assigning array to variable (variant) runtime error 9 please help (ignore correction, this didn't appear for some reason) mark Stephens Microsoft Excel Programming 0 26th Jul 2009 04:52 PM
Excal, Variant and Range witek Microsoft Excel Programming 6 18th Mar 2008 12:13 AM
range and variant Danilo Microsoft Excel Programming 0 26th Jan 2008 03:10 PM
Assigning range to variant vezerid Microsoft Excel Programming 2 12th Mar 2007 04:45 PM
How to define a Range with variant? Yiu Choi Fan Microsoft Excel Programming 6 12th Jul 2004 04:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:03 PM.