+ Reply to Thread
Results 1 to 6 of 6

dynamic arrays, redim preserve

  1. #1
    Registered User
    Join Date
    11-29-2005
    Posts
    12

    dynamic arrays, redim preserve

    I would like to create a dynamic array that has 5 columns, but I do not know how many rows it will have, but it will be something like 40.
    The purpose of this array is to store the relevant Centre of Gravity values for different areas of a car, front suspension, engine, transmission etc.

    This array is populated by going through a variant array, which looks at a bill of materials with around 1500 line items, and those parts that fall into the relevant groupings get picked out, and calculated into the above array.

    What I have done is to create 5 dynamic arrays, and link them together with a common integer, as follows
    Please Login or Register  to view this content.


    Basically, this does not look right, even though it works. Could someone explain if it is possible to have just one array that contains the five columns, and then using the 'redim preserve' function to facilitate this.

    Thank you for your patience in getting this far Also, assume that I am a novice. Thanks for your help.
    Last edited by VBA Noob; 03-19-2007 at 02:24 PM.

  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 Pjjclark,

    The ReDim statement works only on the last array dimension of a mutli-demsional array. You have solved your problem by creating multiple single arrays you can redimension. It would be nice to have a single array you could do this to, but that functionality doesn't exist.

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    11-29-2005
    Posts
    12
    Thanks for that Leith Ross.

    I'll strike it off my 'this is bugging me' list.

    Cheers

    Paul

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Paul

    As it is possible to only redim the last column, one thing you could is is create a 5 row, variable column array. You can then change increase the last column. If you really need the output to be in a 5 column x row format, you can use the worksheet.transpose function to convert.

    I don't know how far you can take it. I did a quick test up to 500 columns and it didn't have any problems.

    hth

    rylo

  5. #5
    Registered User
    Join Date
    11-29-2005
    Posts
    12
    Thanks for that, and I'm intreeged.

    I can see how it would work, the only further help I would appreciate is when it comes to the syntax of the 'redim preserve':

    I assume you would set it up:

    ---

    dim newarray()

    ix as integer

    ix = 1

    redim newarray(5,ix)

    code stuff

    redim Preserve newarray(uboound(newarray(2,ix)+1)

    ---

    It is the above line that I get a 'subscript out of range'

    Thanks

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Say you have data in the range A1:B19.

    Please Login or Register  to view this content.
    The code above will put it into a 2 dimensional array, with 2 rows and a variable number of columns. The first msgbox will show you the upper size of the second dimension (19)

    You can then change it from a 2x19 to a 19x2 array with the transpose.

    The second messagebox shows the ubound of the 1st dimension (20 - includes the default 0 that is not filled)

    Hope that makes sense

    rylo

+ 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