Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

Redim Preserve on loop cause an error of Subscript out of range

$
0
0

Im looping a folder and passing different elements to an array dynamically. When the array is empty and i ReDim it the array increase size but when the array has values and i try to ReDim Preserve i got an erro of:

Subscript out of range

Code:

Dim arrNames As Variant

Do While Len(StrFile) > 0

    'If array is empty
    If Counterarr = 0 Then
        'Add a line to the array
        ReDim arrNames(Counterarr, 1 To 3)
    Else
        'Keep the values and add new
        ReDim Preserve arrNames(Counterarr)
    End If

    'Import document name
    arrNames(Counterarr, 1) = Trim(Replace(Trim(StrFile), ".xlsx", ""))
    'Import Year
    arrNames(Counterarr, 2) = Trim(Left(Trim(Split(Trim(Replace(Trim(StrFile), ".xlsx", "")), "")(1)), 4))
    'Import Month
    arrNames(Counterarr, 3) = Right(Trim(Split(Trim(Replace(Trim(StrFile), ".xlsx", "")), "")(1)), 2)

    Counterarr = Counterarr + 1

    StrFile = Dir

Loop

Image:

When array initialized:

enter image description here

Any advice how to import ArrNames(1) etc?


Viewing all articles
Browse latest Browse all 88835

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>