# Ubound in Array in Array

#### Orongo

##### Board Regular
Hi, I'm trying to get the values of MyArray coded like

Dim MyArray As Variant, dim1 as Integer, dim2 as Integer
ReDim MyArray(1, 1)
MyArray = Array(Array(1, 2, 3), Array(4, 5, 6))
dim1 = UBound(MyArray,1)
dim2 = UBound(MyArray, 2)

Is this evan possible? Thankfull for all good ideas

### Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You can do this:
Code:
``````    Dim MyArray As Variant
MyArray = Array(Array(1, 2, 3), Array(4, 5, 6, 7))

MsgBox LBound(MyArray, 1) & " to " & UBound(MyArray, 1)
MsgBox LBound(MyArray(0), 1) & " to " & UBound(MyArray(0), 1)
MsgBox LBound(MyArray(0), 1) & " to " & UBound(MyArray(1), 1)``````
The first statement makes a two-element array. In the first element, it puts a three-element array, and in the second, a four-element array.

Hi, I'm trying to get the values of MyArray coded like

Dim MyArray As Variant, dim1 as Integer, dim2 as Integer
ReDim MyArray(1, 1)
MyArray = Array(Array(1, 2, 3), Array(4, 5, 6))
dim1 = UBound(MyArray,1)
dim2 = UBound(MyArray, 2)

Is this evan possible? Thankfull for all good ideas
When you assign a nested array like you did above (highlighted in red), it will be addressed differently than a "normal" two-dimensional array. For example, to retrieve the value from 6 from your array, you would do this (assuming your Option Base is set to the default of 0)...

Code:
``MsgBox MyArray(1)(2)``
where the (1) refers to the second array within the Array function... (0) would have referred to the first array (remember, we assumed zero-based arrays) and where the (2) referes to the third element of that second array. So to find the upper and lower bound of MyArray itself, you would use normal methods of addressing; however, if you want the upper and lower bounds of the nested arrays, then you have to address them according to their position within MyArray. The following code demonstrates this (I changed the second nested array to be a different size than the first so you can more easily equate the code with the results it produces)...

Code:
``````Dim MyArray As Variant
Dim lbMyArray As Long, ubMyArray As Long
Dim lbMyArrayElement1 As Long, ubMyArrayElement1 As Long
Dim lbMyArrayElement2 As Long, ubMyArrayElement2 As Long

MyArray = Array(Array(1, 2, 3), Array(4, 5, 6, 7, 8))

lbMyArray = LBound(MyArray)
ubMyArray = UBound(MyArray)

lbMyArrayElement1 = LBound(MyArray(0))
ubMyArrayElement1 = UBound(MyArray(0))

lbMyArrayElement2 = LBound(MyArray(1))
ubMyArrayElement2 = UBound(MyArray(1))

MsgBox lbMyArray & " - " & ubMyArray
MsgBox lbMyArrayElement1 & " - " & ubMyArrayElement1
MsgBox lbMyArrayElement2 & " - " & ubMyArrayElement2``````

0 to 1
0 to 2
0 to 4

How can I get the answer 3 and 5, hence the number of elements in each array?

0 to 1
0 to 2
0 to 4

How can I get the answer 3 and 5, hence the number of elements in each array?
The UBound is not a counting number, it is the index number of the last declared element in the array, so don't expect it to be one. If you need something different from it, then perform a calculation on it. In this case, you would simply add one to the calculated upper bound. But remember, the value for the upper bound (and lower bound for that matter) is dependent on the Option Base setting.

Code:
``````    Dim MyArray As Variant
MyArray = Array(Array(1, 2, 3), Array(4, 5, 6, 7))

MsgBox UBound(MyArray) - LBound(MyArray) + 1 & " elements"
MsgBox UBound(MyArray(0)) - LBound(MyArray(0)) + 1 & " elements"
MsgBox UBound(MyArray(1)) - LBound(MyArray(1)) + 1 & " elements"``````

Replies
4
Views
222
Replies
12
Views
355
Replies
5
Views
210
Replies
0
Views
178
Replies
8
Views
338

1,207,108
Messages
6,076,589
Members
446,215
Latest member
userds5593

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