+ Reply to Thread
Results 1 to 4 of 4

erase an array resulting in array not populated when the macro runs again

  1. #1
    Forum Contributor aprildu's Avatar
    Join Date
    04-13-2014
    Location
    Barrie,On, Canada
    MS-Off Ver
    Excel 2016
    Posts
    153

    erase an array resulting in array not populated when the macro runs again

    I have a two dimentional array that's redim preserved:

    Please Login or Register  to view this content.
    If I don't have "erase ar()" line then if I run a few times (consecutively) then I get the same array stacked multiple times. If I have that line then I don't get any results (if I run multiple times - the first time is OK)

    If I don't run it consecutively then there's no issue. Just wondering why and if it's OK to run multiple times(in case client does it even there's no reason to run multiple times). Any insight is appreciated!
    Last edited by aprildu; 06-14-2019 at 03:00 PM.

  2. #2
    Forum Expert
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    1,389

    Re: erase an array resulting in array not populated when the macro runs again

    For a static array, the erase function will empty the data elements but leave the elements with empty values.
    For a dynamic array, the erase function destroys the array and the empties the memory, requiring a complete rebuild of the array to restore the elements. Since your erase statement is outside the Do loop that builds the array and after the statement that applies the array value, I don't see how it would affect what the array produces. But if it the code posted is actually inside another loop and not shown for this post, then the the behavior could be the result of how the erase function is treating your array.

    Perhaps if you explained what you are trying to do, we could offer an alternative solution without using the erase fuction.
    Last edited by JLGWhiz; 06-14-2019 at 11:12 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, click the 'Go Advanced' button, Then click 'Manage Attachements' and follow the menu.
    Thinking I am smart and knowing I am smart are two entirely different levels of schizophrenia.

  3. #3
    Forum Contributor aprildu's Avatar
    Join Date
    04-13-2014
    Location
    Barrie,On, Canada
    MS-Off Ver
    Excel 2016
    Posts
    153

    Re: erase an array resulting in array not populated when the macro runs again

    I found the issue. I didn't set the x=0 before the loop ( I thought I did in the real code) and that's why it kept adding up. It was driving me bunkers! Thank you for replying!
    Last edited by aprildu; 06-15-2019 at 06:04 PM.

  4. #4
    Forum Expert
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    1,389

    Re: erase an array resulting in array not populated when the macro runs again

    Thanks for the feedback,
    regards, JLG

+ 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