How to make a dynamic array with ReDim and Preserve?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Snowmelt
    New Member
    • May 2015
    • 1

    How to make a dynamic array with ReDim and Preserve?

    I was wondering how I can make an array that takes in integers, but have an endless array space so I don't have to limit my array in a for loop that ends in a large number
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    I would keep a global variable that stores the max subscript for your array. Each time that you need to add another element to the array, just increment that variable and then ReDim your array using the Preserve option.
    Code:
    intMAX_SUBSCRIPT += 1
    ReDim Preserve intMyArray(intMAX_SUBSCRIPT)
    
    intMyArray(intMAX_SUBSCRIPT) = 15  'my new number

    Comment

    Working...