+ Reply to Thread
Results 1 to 7 of 7

Thread: ReDimPreserve vs. Oversizing Array and then shrinking array down....which is better

  1. #1
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    ReDimPreserve vs. Oversizing Array and then shrinking array down....which is better

    Hi,

    As I read books and web posting about excel i have found something i would like to aska question about.

    Dynamic Arrays:
    What i have seen is that some people use the "ReDim Preserve" feature to size the array as needed while others simply "Oversize" the array initially then shrink it down.

    i was wondering why you would "oversize" vs. "ReDim". Is there an advantage to one vs. the other that i dont know about? Is one faster if have lots of data?

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: ReDimPreserve vs. Oversizing Array and then shrinking array down....which is bett

    It's better to minimize the number of times you use Preserve, so it should be quicker to overstate and then resize down.

  3. #3
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Re: ReDimPreserve vs. Oversizing Array and then shrinking array down....which is bett

    You can also use collections as well
    Dim col As New Collection
    col.Add "value1", "key1"  'how to set value and key
    MsgBox col.Item(1)         'how to get value
    MsgBox col.Item("key1") ' -||-
    Collection could be indexed so searching could be faster

    Best Regards
    Last edited by maczaq; 07-01-2011 at 09:29 AM.

  4. #4
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: ReDimPreserve vs. Oversizing Array and then shrinking array down....which is bett

    what is "collection"? How does this work?

  5. #5
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: ReDimPreserve vs. Oversizing Array and then shrinking array down....which is bett

    forgot to say thanks for the posts thus far..........really......learning alot from this site.

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: ReDimPreserve vs. Oversizing Array and then shrinking array down....which is bett

    Look up Collection in the VBA Help.

    You can also use the Scripting.Dictionary object which is more lightweight than the Collection and easier to get an array back out of.

  7. #7
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Re: ReDimPreserve vs. Oversizing Array and then shrinking array down....which is bett

    @romperstomper
    this Dictionary idea is great - I never see this before - thank You

    for all users, you can read about Scripting.Dictionary object here:
    http://www.kamath.com/tutorials/tut009_dictionary.asp

    Best Regards
    MaczaQ

+ 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.2.0