Use the Preserve keyword to keep the values : Dynamic Array « Date Functions « VBA / Excel / Access / Word






Use the Preserve keyword to keep the values

 
Sub ResizePreserve()
    Dim astrNames() As String
    Dim intCounter As Integer
    Dim vntAny As Variant

    ReDim astrNames(1)
    astrNames(0) = "A"
    astrNames(1) = "B"

    ReDim Preserve astrNames(3)
    astrNames(2) = "C"
    astrNames(3) = "D"

    For Each vntAny In astrNames
        Debug.Print vntAny
    Next vntAny
End Sub

 








Related examples in the same category

1.Dynamic Arrays
2.Declaring and Working with Dynamic Arrays
3.Use ReDim to create dynamic array
4.Using a Dynamic Array
5.There's a potential problem when you try to resize the array:
6.A destroying the old values by using the Preserve keyword
7.Transpose() sub procedure is using a dynamic array that is re-dimensioned with two dimensions.