+ Reply to Thread
Results 1 to 5 of 5

Dynamic Array Fails to Redim Preserve

  1. #1
    Registered User
    Join Date
    04-16-2013
    Location
    Tennessee
    MS-Off Ver
    Excel 2010
    Posts
    10

    Dynamic Array Fails to Redim Preserve

    I am not posting my workbook. It contains significant personal data. The attached JPG gives an indication of the nature of the data the workbook contains.

    The two Redim Perserve statements in the subroutine RedimPerserve() are intended to keep the upper bounds of both arrays synchronized. The first statement for RowNumber() works fine. The second statement for ColumnContent() fails with "Subscript out of range". The data in the attached JPG shows that the breakpoint data stored by VBA should allow the second statement to successfully function. I am doing the Redim Preserve on the last dimension of the array.

    Why is this error message presenting? What can I do to fix it?

    Rod

    For me to believe is insufficient for you to know. - rodalsa
    Attached Images Attached Images

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Dynamic Array Fails to Redim Preserve

    "Subscript out of range".
    Could mean a number of things, but most likely guess would be your reference to worksheet, or range is wrong. Since you have an array, it is most likely that the range you are selecting is out of boundary, so check the array dim and upper and lower boundaries.

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Dynamic Array Fails to Redim Preserve

    if you use redim preserve (rather than just redim) you can only change the boundaries of the last dimension-you've got to transpose your data or maybe use an array of arrays. the best option depends on what you're gonna do with the array
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Dynamic Array Fails to Redim Preserve

    You can only redim preserve the final dimension of an array, consider:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-16-2013
    Location
    Tennessee
    MS-Off Ver
    Excel 2010
    Posts
    10

    Thumbs up Re: Dynamic Array Fails to Redim Preserve

    Thanks for the quick replies.

    AB33:

    Public RowNumber() As String
    Public ColumnContent() As Variant

    These two arrays are Redim Preserve(d) to increase the size of the array in the following code segment;

    Please Login or Register  to view this content.
    While digging around on your suggestion I uncovered the following solution to my problem;

    I found that the redim preserve code for increasing the size of both of the columncontent() array's dimension were not stated as 1 to x. See above code. I changed both dimension to 1 to x as in;

    Please Login or Register  to view this content.
    I then ran the code without modifying the Redim Preserve statements that reduced the last dimension. That statement was;

    Please Login or Register  to view this content.
    The statement failed. I changed the first dimension to 1 to Ubound(ColumnContent,1) shown here;

    Please Login or Register  to view this content.
    and the statement no longer produced an error. Problem solved if after the move to End Sub (the next sequential statement) these debug.prints check out...

    ' BEFORE the correcting code revisions and unable to get to the next sequential code line due to "Subscript out of range" ' error...

    Debug.Print ColumnContent(3,12)
    ' Returns - "Runtime error '9':"
    ' "Subscript out of range" as it should because Ubound(ColumnContent,2) was 11 and

    Debug.Print ColumnContent(3,11) 'returns
    1.0 Biopsy skin lesion (11100) 'as expected because Ubound(ColumnContent,2) was 11 and

    Debug.Print ColumnContent(3,10) 'returns
    1.0 Routine venipuncture (3415) 'as expected 10 < 11.

    'AFTER the correcting code revisions and in the break mode on the next sequential line..

    Debug.Print ColumnContent(3,12)
    ' Returns - "Runtime error '9':"
    ' "Subscript out of range" as it should because Ubound(ColumnContent,2) is now 10 and

    Debug.Print ColumnContent(3,11) ' now
    ' Returns - "Runtime error '9':"
    ' "Subscript out of range" as it should because Ubound(ColumnContent,2) is now 10 and

    Debug.Print ColumnContent(3,10) 'returns
    1.0 Routine venipuncture (3415) 'as expected 10 = 10.

    ' Clearly showing that the Redim Preserve now works as expected.

    I should have known. These masters of humanity insist on uniformity. Redim Preserve with the dimensions stated as x to y first and you better Redim Preserve with the dimensions as x to y everywhere else.

    Hey. Thanks again. I am marking this thread solved and bumping your *.

    Rod

    For me to believe is insufficient for you to know. - rodalsa

+ 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