How to view the dimension of an open array in VBA ?

B

baj

Hi All,
Suppose you create an open array (1 dimension) :
Dim arr() as String

Then functions like Ubound(arr) or Lbound(arr) will give an error on
the subscript...

With which function or method is it possible to know if the array arr
has got some dimensions or not ?
I want to know this because I want enlarge the array when I need it...
that's not diffcult with the combination 'Preserve Redim' but I have to
know the starting point...

If arr() has just been dimensioned and you want to add 3 elements then
you have to Redim arr(3),
But when arr() has already 5 elements you have to say Preserve Redim
arr(8)

One should expect you can check this dimension with Ubound or LBound...
but this isn't the case if the dimension is actualy not existing...

How, How ?

TXS for any help
 
P

paul.robinson

Hi
Hard to see why you would check the size of an array without declaring
its size first...maybe your code sequencing needs a rethink?
That said, how about

on error resume next
err.clear
ArraySize = UBound(arr)
If err.number<>0 then
'the redim bit
Else
'the dim bit
end if
on error goto 0

regards
Paul
 
A

Andrew Taylor

My usual technique in this situation is to force arr to be an
"empty array" by using:

arr = split("")

This makes UBound(arr) = -1, so you test for this to see
if arr contains any data yet.

hth
Andrew
 
A

Alan Beban

baj said:
Hi All,
Suppose you create an open array (1 dimension) :
Dim arr() as String

Then functions like Ubound(arr) or Lbound(arr) will give an error on
the subscript...

With which function or method is it possible to know if the array arr
has got some dimensions or not ?
I want to know this because I want enlarge the array when I need it...
that's not diffcult with the combination 'Preserve Redim' but I have to
know the starting point...

If arr() has just been dimensioned and you want to add 3 elements then
you have to Redim arr(3),
But when arr() has already 5 elements you have to say Preserve Redim
arr(8)

One should expect you can check this dimension with Ubound or LBound...
but this isn't the case if the dimension is actualy not existing...

How, How ?

TXS for any help
If the functions in the freely downloadable file at
http://home.pacbell.net/beban are available to your workbook, then

ArrayDimensions(arr) will return 0 if the array has not yet been sized.

Alan Beban
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top