PC Review


Reply
Thread Tools Rate Thread

arrays from Excel VBA

 
 
MJH
Guest
Posts: n/a
 
      19th Dec 2006
I wrote a sub in VBA for Excel that accepts either 1) a selected range
directly from the active worksheet, or 2) from an created array (have used
both "as Double" and "as Variant") by another routine, which in turn
uses the same selected range. In my sub, there is an "if" statement
(>=) which compares every value of the array of numbers to a set
(const) value. I get the same result, no matter where the array comes
from (created or selected) *except* when one of the const values happen
to *equal* one of the array numbers. For some reason, if the const
value equals one of the created (Double or Varient) array values, it
continues to loop one more time, as if the numbers were not equal?!

I guess the question boils down to this: when VBA uses a selected
range as an array, what type of variable is it? If the selection is a
range of numbers, why would they not equal their equivalent in Double
or Variant form? What changes when I convert a spreadsheet range to an array
of values in VBA?




thanks


 
Reply With Quote
 
 
 
 
RB Smissaert
Guest
Posts: n/a
 
      19th Dec 2006
Not sure it explains your problem, but and array based
on a worksheet range is always a Variant array.

So you do:

Dim arr

arr = Range(cells(1), cells(20,20))

and you will get a Variant array.

RBS


"MJH" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I wrote a sub in VBA for Excel that accepts either 1) a selected range
> directly from the active worksheet, or 2) from an created array (have used
> both "as Double" and "as Variant") by another routine, which in turn
> uses the same selected range. In my sub, there is an "if" statement
> (>=) which compares every value of the array of numbers to a set
> (const) value. I get the same result, no matter where the array comes
> from (created or selected) *except* when one of the const values happen
> to *equal* one of the array numbers. For some reason, if the const
> value equals one of the created (Double or Varient) array values, it
> continues to loop one more time, as if the numbers were not equal?!
>
> I guess the question boils down to this: when VBA uses a selected
> range as an array, what type of variable is it? If the selection is a
> range of numbers, why would they not equal their equivalent in Double
> or Variant form? What changes when I convert a spreadsheet range to an
> array of values in VBA?
>
>
>
>
> thanks
>
>


 
Reply With Quote
 
RB Smissaert
Guest
Posts: n/a
 
      19th Dec 2006
But when the array comes from a sheet range like this:

Dim arr
arr = range(cells(1), cells(20,20))

then it will always be a 1-based, 2-D (even when there is only one column in
the range) variant array.

RBS

"PapaDos" <(E-Mail Removed)> wrote in message
news:14C3BC8A-0366-4934-B576-(E-Mail Removed)...
> Hard to guess what you talk about without seeing your code and variables
> declarations.
>
> But I guess you have a problem with indexing, by default VBA arrays start
> at
> 0 and ranges start at 1. So using my_variable(1) is referencing the first
> item in a range but the second item in an array...
>
> Look at the "Option Base" statement or change the way you declare your
> arrays, forcing them to start at 1...
>
> --
> Regards,
> Luc.
>
> "Festina Lente"
>
>
> "MJH" wrote:
>
>> I wrote a sub in VBA for Excel that accepts either 1) a selected range
>> directly from the active worksheet, or 2) from an created array (have
>> used
>> both "as Double" and "as Variant") by another routine, which in turn
>> uses the same selected range. In my sub, there is an "if" statement
>> (>=) which compares every value of the array of numbers to a set
>> (const) value. I get the same result, no matter where the array comes
>> from (created or selected) *except* when one of the const values happen
>> to *equal* one of the array numbers. For some reason, if the const
>> value equals one of the created (Double or Varient) array values, it
>> continues to loop one more time, as if the numbers were not equal?!
>>
>> I guess the question boils down to this: when VBA uses a selected
>> range as an array, what type of variable is it? If the selection is a
>> range of numbers, why would they not equal their equivalent in Double
>> or Variant form? What changes when I convert a spreadsheet range to an
>> array
>> of values in VBA?
>>
>>
>>
>>
>> thanks
>>
>>
>>


 
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
Arrays in Excel Griffey5 Microsoft Excel Crashes 1 6th Jan 2008 11:43 PM
Excel VBA & Arrays Robert Microsoft Excel Programming 3 24th Oct 2006 03:34 PM
excel arrays kenrock Microsoft Excel Programming 6 12th Nov 2005 11:36 AM
arrays in excel =?Utf-8?B?RGFu?= Microsoft Excel Worksheet Functions 9 24th May 2005 07:01 PM
Arrays in Excel VBA Eliezer Microsoft Excel Programming 4 3rd Sep 2004 08:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:57 AM.