+ Reply to Thread
Results 1 to 5 of 5

Redim Preserve a multi-dimen. array.

  1. #1
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    Redim Preserve a multi-dimen. array.

    When I execute the code below, the value of the "element" variable at the last line equals to 1 instead of 3. Can somebody tell me why?

    The variable "element" at the last line would equals to 3 if I don't use the preserve keyword with the redim statement.

    Please Login or Register  to view this content.
    Last edited by Leith Ross; 10-07-2012 at 08:28 PM. Reason: Added Code Tags

  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

    Re: Redim Preserve a multi-dimen. array.

    Hello Today david90 ,

    The ReDim statement only affects the last dimension of a multiple dimension array.
    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!)

  3. #3
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    Re: Redim Preserve a multi-dimen. array.

    are u sure? When I run this code:

    ReDim array2(10, 5)
    Debug.Print UBound(array2, 1)
    Debug.Print UBound(array2, 2)
    Debug.Print " "

    ReDim array2(5, 3)
    Debug.Print UBound(array2, 1)
    Debug.Print UBound(array2, 2)
    Debug.Print " "
    I get 10 5 and 5 3 from the debug.print statement.

    However, when I run this code:
    ReDim Preserve array2(10, 5)
    Debug.Print UBound(array2, 1)
    Debug.Print UBound(array2, 2)
    Debug.Print " "

    ReDim Preserve array2(5, 3)
    Debug.Print UBound(array2, 1)
    Debug.Print UBound(array2, 2)
    Debug.Print " "
    I get 10 5 and 10 5. Seems like redim preserve doesn't resize any dimension of a multidimensional array.

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Redim Preserve a multi-dimen. array.

    Try this maybe it will help you see what Leith was trying to point out. You cannot resize the first dimension of an array

    Please Login or Register  to view this content.
    Last edited by mike7952; 10-08-2012 at 03:46 PM.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Redim Preserve a multi-dimen. array.

    david

    The value is 1 because there's an error on this line which is repeated twice in the code.
    Please Login or Register  to view this content.
    The error iscaused by trying to redimension the first element of the array which can't be done, as Leith and Mike have already said

    The On Error Resume Next hides the error and VBA just continues on with the rest of the code.

+ 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