+ Reply to Thread
Results 1 to 13 of 13

How to Preserve a Dynamic Array When the Array is Created

  1. #1
    Registered User
    Join Date
    10-03-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    44

    How to Preserve a Dynamic Array When the Array is Created

    Hi All,

    I am trying to create an arrary from the column headers names in row 1. That step works ok.
    Once the array is declared and fills with the column header names I do not want the array to change. I want the values preserved as this will go into a loop and perform some actions that cause the column headers names to change, but I need the arrary to stay the same as when it was declared.

    Here is my coding for this part:


    Please Login or Register  to view this content.
    The problem line is the ReDim Preserve..... Excel states "Subscript out of range".

    A side question:
    1 to 24 represents all the columns names or range in the arrary.
    When I declared my arrary in the locals window I noticed it in type field as Variant (1 to 1, 1 to 24). Why is Excel viewing this as 2 arrays? I thought it would just be Variant ( 1 to 24).


    Thanks in advance for the assistance.

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

    Re: How to Preserve a Dynamic Array When the Array is Created

    It's not, it's a 2 dimensional array with one row and many columns.

    You can't do what you are trying to, you can only change the last dimension of a dynamic array, not both as you are trying to - I suspect you're making things more complicated than they need to be. What are you actually trying to do?>

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to Preserve a Dynamic Array When the Array is Created

    1.) A Range that is read into an Array (using the Variant = Range method) is always two dimensions even when the range is one row or one column.

    2.) ReDim Preserve only allows the last dimension of a multidimensional array to be changed (ReDimed).


    There are ways around both of those limits depending on what you want to do. It usually takes a slightly different method\approach in the code.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

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

    Re: How to Preserve a Dynamic Array When the Array is Created

    Why are you redimensioning the array?
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    10-03-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: How to Preserve a Dynamic Array When the Array is Created

    Yes, I can use different methods to work around this, but I thought maybe I was seeing something incorrect. My general question would then be if I create a dynamic arrary and once the values are applied to the array how can I preserve the values in that array to stay the same for the entire module?

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

    Re: How to Preserve a Dynamic Array When the Array is Created

    The array isn't dynamic.

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to Preserve a Dynamic Array When the Array is Created

    Quote Originally Posted by Excel Guy 123 View Post
    Yes, I can use different methods to work around this, but I thought maybe I was seeing something incorrect. My general question would then be if I create a dynamic arrary and once the values are applied to the array how can I preserve the values in that array to stay the same for the entire module?
    Are you asking how to increase the row dimension of an array created from a Range? If yes, one method is to transpose the array so that the rows dimension is the 2nd dimension. Then ReDim Preserve works when you increase the row dimension.

    Example:

    Please Login or Register  to view this content.

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

    Re: How to Preserve a Dynamic Array When the Array is Created

    I think you may have misunderstood what preserve does. It doesn't stop the values changing, it just doesn't delete them when you make the array larger

  9. #9
    Registered User
    Join Date
    10-03-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: How to Preserve a Dynamic Array When the Array is Created

    Thanks for the sample code and explanations.

    Basically what I am trying to accomplish is to create an array from the column headers names then use the column headers names in the array and loop through all of them and use a select case that executes code based on the column name. The code sometimes will delete or move a column and this could change the array, which I don't want to happen.

    I guess I could transfer the array to a string array, but that seems to be significant coding.

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

    Re: How to Preserve a Dynamic Array When the Array is Created

    If you delete/move a column the array will remain unchanged.

    All the array contains are the values the cells in the range contained when you populated it.

  11. #11
    Forum Contributor
    Join Date
    01-25-2011
    Location
    Virginia
    MS-Off Ver
    Excel 2016
    Posts
    166

    Re: How to Preserve a Dynamic Array When the Array is Created

    Deleting of moving a column will not change the array. To get an intelligent answer you need to show your code and (if possible) show the line that causes the array to change.
    Tom

    If my answer resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post
    .

  12. #12
    Registered User
    Join Date
    10-03-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: How to Preserve a Dynamic Array When the Array is Created

    Here is the module. I want the array/string to hold the column headers names. I do not want to hard code the values in case the reporting output changes. At this point I am only wanting to rearrange the columns in a certain order then more coding to come after.

    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    01-25-2011
    Location
    Virginia
    MS-Off Ver
    Excel 2016
    Posts
    166

    Re: How to Preserve a Dynamic Array When the Array is Created

    If you are calling this procedure from another procedure (which it would appear is true) each time you call it the StrAryColumnNames array is reestablished. To prevent this you can use Static instead of dim and then do an if check to only populate it the first time. Something like the below after the dim statements:
    Please Login or Register  to view this content.
    I must admit that I don't fully understand the "life" of static variables, so I recommend you step though the code on a "test" workbook before you go live.

    Regards,

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] redimension and preserve dynamic array
    By williams485 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-30-2013, 09:05 AM
  2. Loop new messages containing a table, populate a dynamic array, paste array to Excel
    By laripa in forum Outlook Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2013, 07:20 AM
  3. [SOLVED] Dynamic Array Fails to Redim Preserve
    By rodalsa1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-07-2013, 09:52 AM
  4. [SOLVED] Populate Listbox with all rows of a dynamic array where elements of a single array match.
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-07-2012, 04:54 AM
  5. dynamic array redim preserve: run-time error 9, Subscript out of range.
    By Broesel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2009, 08:39 AM

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