Consulting

Results 1 to 4 of 4

Thread: Determing Array Size

  1. #1
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location

    Determing Array Size

    Is there a way to dynamically determine the size of an array. In particular, assume that the parent proc has dimensioned an array, X, at 5, e.g.,

    Dim X(5) as Single

    The parent then calls SubA and passes X as one of the arguements. Is there a way for SubA to "figure out" the size of X so that if the # of values to be stuffed in X exceeds the original dimension, SubA would stop and perhaps display an error message.

    Solutions already considered:
    1. pass the length of X to SubA
    2. trap the error in SubA, e.g., On Error ...

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Sub DoThis()
          Dim MyArray(1 To 20) As Long
          MsgBox "MyArray is " & LBound(MyArray) & " to " & UBound(MyArray)
    End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by johnske

    Sub DoThis()
          Dim MyArray(1 To 20) As Long
          MsgBox "MyArray is " & LBound(MyArray) & " to " & UBound(MyArray)
    End Sub
    Thanks. This seems almost too easy; but it works

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by MWE
    Thanks. This seems almost too easy; but it works
    It was - It does. Have a nyce day
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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