+ Reply to Thread
Results 1 to 6 of 6

subscript out of range

  1. #1
    lvcha.gouqizi
    Guest

    subscript out of range

    I define a type

    Type Change

    nextChanges() As Integer
    nextNum As Integer
    prevChanges() As Integer
    prevNum As Integer
    End Type


    and an array of type Change
    Dim myChanges() as Change

    when I fill in data to myChanges like
    myChanges(index).nextChanges(subIndex) = ***

    It always says "subscript out range". I checked that the index is 1
    and subIndex is 10. What't the reason for this?

    thank.


  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello lvcha.gouqizi,

    When using dynamic arrays, arrays with empty (), you must use the ReDim statement to set the element size of the array. If you have not set the size of the array, the system will give you subscript out of range.

    Your UDT (User Defined Type) contains 2 dynamic arrays which must be set to some size before you try to store a value to them. ReDim allows you to change the number of elements as needed, but you must change the array size before trying to store or retrieve a value to avoid the subscript out of range error.

    Sincerely,
    Leith Ross

  3. #3
    Jim Thomlinson
    Guest

    RE: subscript out of range

    This worked for me in a regular module...

    Type Change
    nextChanges() As Integer
    nextNum As Integer
    prevChanges() As Integer
    prevNum As Integer
    End Type

    Sub test()
    Dim myChanges() As Change

    ReDim myChanges(5)
    ReDim myChanges(1).nextChanges(5)

    myChanges(1).nextChanges(1) = 1
    myChanges(1).nextChanges(2) = 2
    myChanges(1).nextChanges(3) = 3
    myChanges(1).nextChanges(4) = 4

    MsgBox myChanges(1).nextChanges(1)
    MsgBox myChanges(1).nextChanges(2)
    MsgBox myChanges(1).nextChanges(3)
    MsgBox myChanges(1).nextChanges(4)

    End Sub

    You will have to post more code if you need more help...
    --
    HTH...

    Jim Thomlinson


    "lvcha.gouqizi" wrote:

    > I define a type
    >
    > Type Change
    >
    > nextChanges() As Integer
    > nextNum As Integer
    > prevChanges() As Integer
    > prevNum As Integer
    > End Type
    >
    >
    > and an array of type Change
    > Dim myChanges() as Change
    >
    > when I fill in data to myChanges like
    > myChanges(index).nextChanges(subIndex) = ***
    >
    > It always says "subscript out range". I checked that the index is 1
    > and subIndex is 10. What't the reason for this?
    >
    > thank.
    >
    >


  4. #4
    Jim Thomlinson
    Guest

    Re: subscript out of range

    You will also get this error if you try to fill the 11th item of a 10 item
    array. The array may be initalized but if it is too small then you are
    hooped. Unlike C which will allow you to write outside the boundaries of the
    array which is a horror I am glad to be done with.
    --
    HTH...

    Jim Thomlinson


    "Leith Ross" wrote:

    >
    > Hello lvcha.gouqizi,
    >
    > When using dynamic arrays, arrays with empty (), you must use the ReDim
    > statement to set the element size of the array. If you have not set the
    > size of the array, the system will give you subscript out of range.
    >
    > Your UDT (User Defined Type) contains 2 dynamic arrays which must be
    > set to some size before you try to store a value to them. ReDim allows
    > you to change the number of elements as needed, but you must change the
    > array size before trying to store or retrieve a value to avoid the
    > subscript out of range error.
    >
    > Sincerely,
    > Leith Ross
    >
    >
    > --
    > Leith Ross
    > ------------------------------------------------------------------------
    > Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
    > View this thread: http://www.excelforum.com/showthread...hreadid=480258
    >
    >


  5. #5
    lvcha.gouqizi
    Guest

    Re: subscript out of range

    Thanks all.

    So what's the use of Redim Preserve?

    I still get "subscript out of range" error when I modified my code to
    the below

    Type Change
    nextChanges() As Integer
    nextNum As Integer
    prevChanges() As Integer
    prevNum As Integer
    End Type

    Dim myChanges() as Change

    Redim myChanges(20)
    Dim i as integer
    for i = 1 to 20
    Redim myChanges(i).nextChanges(50)Type Change
    Redim myChanges(i).prevChanges(50)Type Change
    next i


    .......
    myChanges(index).nextChanges(subIndex) = ***
    Redim preserve myChanges(index).nextChanges(subIndex)


    The error is reported in the Redim preserve line. What's wrong with it?


  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello lvcha.gouqizi,


    Dim myChanges() as Change

    ReDim myChanges(20)
    ReDim nextChanges(50)
    ReDim prevChanges(50)

    Dim i as integer

    for i = 1 to 20
    myChanges(i).nextChanges(50)Type Change
    myChanges(i).prevChanges(50)Type Change
    next i

    In this line where do you define and assign the values to Index and SubIndex
    myChanges(index).nextChanges(subIndex) = ***

    This line saves the changes to the Array's data held the last dimension. Redim only affects the last dimension of an array. Other indexes remain unchanged.
    Redim preserve myChanges(index).nextChanges(subIndex)

    Sincerely,
    Leith Ross
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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