Solved: Need a tutorial on Array Redim Preserve
Good Day,
I've been playing with arrays (for hours).
Can someone explain why the first code works, and the second one does not?
I noticed that if put the variable/expanding portion as the second array, it works.
[VBA]Sub DoesWork()
Dim x() As Variant
Dim i
Dim j
For i = 1 To 10
If Cells(i, 1) Mod 2 = 0 Then
j = j + 1
ReDim Preserve x(1 To 1, 1 To j)
x(1, j) = Cells(i, 1).Value
End If
Next i
Range("B1").Resize(UBound(x, 2)) = Application.Transpose(x)
End Sub
Sub DoesNotWork()
Dim x() As Variant
Dim i
Dim j
For i = 1 To 10
If Cells(i, 1) Mod 2 = 0 Then
j = j + 1
ReDim Preserve x(1 To j, 1 To 1)
x(1, j) = Cells(i, 1).Value
End If
Next i
Range("B1").Resize(UBound(x, 2)) = Application.Transpose(x)
End Sub[/VBA]