+ Reply to Thread
Results 1 to 6 of 6

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

  1. #1
    baj
    Guest

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

    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


  2. #2

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

    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


  3. #3
    Dave Peterson
    Guest

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

    redim preserve arr(lbound(arr) to ubound(arr)+3)

    ????



    baj wrote:
    >
    > 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


    --

    Dave Peterson

  4. #4
    Andrew Taylor
    Guest

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

    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


    baj wrote:
    > 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



  5. #5
    Alan Beban
    Guest

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

    baj wrote:
    > 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

  6. #6
    baj
    Guest

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

    Hi,

    Many Thanks to all the contributors to answering my question.

    Baj


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1