In VBA, I am storing values into a dynamic array called PidArr in a function called Unlock. The array starts like:
Dim PidArr() As String
ReDim PidArr(1 To 2)
The data getting inserted looks like:
...within a loop
PidArr(Count) = LineStr 'this is a string containing the data.
Logging "Inserted " & PidArr(Count) 'this correctly shows the data has inserted.
ReDim PidArr(1 To Count + 1) 'resize the array more
After correctly storing values into that array, another function (Advance) is later called (and passes the PidArr array into it)
Advance listRecords:=PidArr
The function looks like:
Sub Advance(ByRef listRecords() As String)
In Advance I can print something like:
UBound(listRecords)
And it returns a 6.
But when I try to print out the values like listRecords(1) or listRecords(2) etc, nothing gets printed out (blank).
Why is this? It doesn’t crash, so it’s not an invalid range in the array at all.
You use need to
redim preserve. What you’ve done by just using redim is redimensioned (redeclared) the array with a new size and truncated the old data. So you just have elements sitting there with nothing in them.preservewill hold onto the data in the elements when youredim.