# Array size question

#### Mr930

##### Well-known Member
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Personally I use UBound() to find the end of the array. 2 reasons:

1. I didn't know .Count existed
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.

Are you sure arrayname.Count works?

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.

.Count seems to work, but I see UBound used a lot so I wanted to check.

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....

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.

What are x and y arrays of?

Rick

Interop? Is this VB.Net or something?

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....

Replies
5
Views
547
Replies
13
Views
1K
Replies
5
Views
340
Replies
5
Views
326
Replies
4
Views
434

1,211,710
Messages
6,103,450
Members
447,865
Latest member
Devsito

### 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.

### Which adblocker are you using?

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

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