size of multidimensional dynamic array

  • Thread starter Thread starter ThatFella
  • Start date Start date
T

ThatFella

Having created a dynamic, multidimensional array like so:

Dim gInfo() As String
ReDim gInfo(0 To 1, 0 To 1)

I understand that you can only change the size of the last dimension.
Once having created an array, though, how do you find the size of this
second dimension?

The following line would not compile ("expected Array"):

iSize = UBound(gInfo(1))

I expected gInfo(x) to return the xth array in this "array of arrays".
Is this not so in VBA? How then do you get the size of the second
dimension? Thanks in advance.
 
Where is the code that made gInfo(1) an array? It isn't in your post,
and VBA also seems to think that gInfo is not an array. And in any
event, gInfo is declared as a 2-D array, so gInfo(1) doesn't refer to
anything, unlike gInfo(0,1) or gInfo(1,0) or gInfo(1,1).

If all the code being executed is
Dim gInfo() As String
ReDim gInfo(0 To 1, 0 To 1)
iSize = UBound(gInfo(1))

then gInfo is not an array of arrays, and you should change gInfo(1) to
gInfo,2.

Alan Beban
 
By the way, you should be more precise in your use of "size"; do you
mean the upper bound? Or the number of elements accommodated in that
"direction"? In your case, if gInfo has not been changed from its
original declaration, the upper bound of the "column" dimension is 1,
but the number of "columns" is 2.

Alan Beban
 
-Originally posted by Alan Beban -

<snip>

then gInfo is not an array of arrays, and you should change gInfo(1)
to
gInfo,2.

Alan Beban



Guess I haven't read the documentation of UBound recently.
UBound(gInfo, 2) is what I was looking for. Thanks!

In response to your comment about size, I didn't really care which way
it was taken; either answer would have sufficed, but I'll be more
precise next time. But since you bring it up, how DO you get actual
length of the array (not UBound)? Thanks.
 
Typically,

for 1-dimensional arrays:
iSize=UBound(arr) - LBound(arr) + 1

for 2-dimensional arrays:
iSize = (UBound(arr, 1) - LBound(arr, 1) + 1) * _
(UBound(arr, 2) - LBound(arr, 2) + 1)

Alan Beban
 
Back
Top