ExoticBirdsMerchant
ExoticBirdsMerchant

Reputation: 1496

Issue regarding the upper index number of an array when using Redim Preserve Statement at VBA

Just a quick question on VBA Arrays...

What happens if I use the ReDim Preserve statement and I resize an Array by decreasing the Upper index number. If myArray(5) ="Shmoo" what if I do that:

ReDim Preserve myArray(2)

Can it be done since the Preserve must not loose the data that are tied to each index number in the array and index number 5 in the array has the String value "Smoo" ?

Upvotes: 1

Views: 114

Answers (1)

Olle Sjögren
Olle Sjögren

Reputation: 5385

The data in myArray(3), myArray(4) and myArray(5) will be lost. Preserve only keeps the data that fits within the new size specified with the ReDim statement.

From the Excel VBA help file:

If you make an array smaller than it was, data in the eliminated elements will be lost.

Upvotes: 3

Related Questions