Array size question

Mr930

Well-known Member
Joined
Aug 31, 2006
Messages
585
I am passing an array into a function, where I do some calculations by looping through it. Is there any speed difference, or preference, between using
arrayname.Count vs. UBound(arrayname) to find the end of the array?

thanks
Fred Emmerich
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Personally I use UBound() to find the end of the array. 2 reasons:

1. I didn't know .Count existed :rolleyes:
2. If you have a strangely sized array, i.e. an array with elements from 10 to 100, UBound will return 100 whereas .Count will return 91 (I think). Therefore .Count doesn't return the end of the array in my opinion.
 
Upvote 0
Are you sure arrayname.Count works?
 
Upvote 0
In code like
Code:
Dim rng1 as Range, myArray as Varint

Set rng1 = Range("A1:A10")

myArray = rng1.Value

For i = 1 to ???
I prefer to use UBound(myArray, 1) rather than rng1.Count. On the logic that if I'm working on myArray, I should refer to myArray.

For example, if I later find that I need to Redim Preserve myArray before looping, then I don't need to edit the loop code.
 
Upvote 0
I am passing in a range to my function:

Function Interp(dTarget As Double, x, y)

I am trying to find the size of the x and y arrays. UBound(x) gives an error. x.Count gives me the size of the array. Can't figure out why UBound is not working....
 
Upvote 0
I am passing in a range to my function:

Function Interp(dTarget As Double, x, y)

I am trying to find the size of the x and y arrays. UBound(x) gives an error. x.Count gives me the size of the array. Can't figure out why UBound is not working....

Are you calling your Interp function from other VB code or from within a worksheet formula? Also, show us all of your Interp function's code so we can see what is going on.
 
Upvote 0
Rick

Interop? Is this VB.Net or something?
 
Upvote 0
Rick

Interop? Is this VB.Net or something?

It does sound like that, doesn't it; but I said Interp, not Interop. Here is the section I quoted from the OP in my message that you are referring to (note the part highlighed in red)...

I am passing in a range to my function:

Function Interp(dTarget As Double, x, y)

I am trying to find the size of the x and y arrays. UBound(x) gives an error. x.Count gives me the size of the array. Can't figure out why UBound is not working....
 
Upvote 0

Forum statistics

Threads
1,219,162
Messages
6,146,660
Members
450,706
Latest member
LGVBPP

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top