Ubound in Array in Array

Orongo

Board Regular
Joined
Nov 6, 2011
Messages
83
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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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.
 
Upvote 0
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
 
Upvote 0
Thank you for the reply. I get the answer

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?
 
Upvote 0
Thank you for the reply. I get the answer

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.
 
Upvote 0
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"
 
Upvote 0

Forum statistics

Threads
1,224,878
Messages
6,181,529
Members
453,053
Latest member
DavidKele

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