PDA

View Full Version : Solved: Redim Preserve



slamet Harto
07-10-2008, 09:18 PM
Dear Guys,

Does any one know or have any link of article and sample for vba synthax "Redim Preserve"?

I need to learn more on this code.

Thanks & Rgds,
Harto

mikerickson
07-10-2008, 10:13 PM
This shows some of the ways that ReDim Preserve works. (Changing the type of myArray will show you the initial values of different data types.)Sub demo()
Dim myArray() As Long

ReDim myArray(1 To 5)
MsgBox "First" & vbCr & arrayInfo(myArray)

myArray(1) = 11
myArray(2) = 22
myArray(3) = 33
myArray(4) = 44
myArray(5) = 55
MsgBox "Second" & vbCr & arrayInfo(myArray)

ReDim Preserve myArray(1 To 3)
MsgBox "Third" & vbCr & arrayInfo(myArray)

ReDim Preserve myArray(1 To 6)
MsgBox "Fourth" & vbCr & arrayInfo(myArray)

ReDim myArray(1 To 4)
MsgBox "Fifth" & vbCr & arrayInfo(myArray)

End Sub

Function arrayInfo(someArray) As String
Dim i As Long
For i = LBound(someArray) To UBound(someArray)
arrayInfo = arrayInfo & "," & someArray(i)
Next i
arrayInfo = "This array has indicies " & LBound(someArray) & " to " & UBound(someArray) & vbCr & Mid(arrayInfo, 2)
End Function

In multi-demensional arrays, only the last index can be ReDimmed Preserve

ReDim myArray(1 to 10, 1 to 5)
ReDim Preserve myArray(1 to 10, 1 to 3):Rem OK
ReDim Preserve myArray(1 to 3, 1 to 3):Rem throws error.

Bob Phillips
07-11-2008, 12:34 AM
Have you tried VBA help? If you had you wouldn't need to ask, if you hadn't, then you should.

slamet Harto
07-13-2008, 09:28 PM
Thanks Bob
Forgot to look into VBA help and thanks for the code. This will help me to understand.

Highly appreciate it.
Rgds, Harto