L
Lionel H
MS VBA Help has an article entitled
Using For Each...Next Statements
Which claims
The following code loops through each element in an array
and sets the value of each to the value of the index variable I.
Dim TestArray(10) As Integer, I As Variant
For Each I In TestArray
TestArray(I) = I
Next I
I can't make this work.
I extended the code to read
Sub MS_For_Each_Element_In_Array_Demo()
Dim TestArray(2) As Integer, I As Integer, Element As Variant
For Each Element In TestArray: Debug.Print Element;: Next Element:
Debug.Print
For Each Element In TestArray
TestArray(Element) = I
Debug.Print TestArray(Element);
I = I + 1
Next Element
Debug.Print
For Each Element In TestArray: Debug.Print Element;: Next Element:
Debug.Print
End Sub
Which delivered this to the immediate window:
0 0 0
0 1 2
2 0 0
Is it actually possible to make the For Each…Next construct correctly
populate the elements of an array, and if so, how please?
Using For Each...Next Statements
Which claims
The following code loops through each element in an array
and sets the value of each to the value of the index variable I.
Dim TestArray(10) As Integer, I As Variant
For Each I In TestArray
TestArray(I) = I
Next I
I can't make this work.
I extended the code to read
Sub MS_For_Each_Element_In_Array_Demo()
Dim TestArray(2) As Integer, I As Integer, Element As Variant
For Each Element In TestArray: Debug.Print Element;: Next Element:
Debug.Print
For Each Element In TestArray
TestArray(Element) = I
Debug.Print TestArray(Element);
I = I + 1
Next Element
Debug.Print
For Each Element In TestArray: Debug.Print Element;: Next Element:
Debug.Print
End Sub
Which delivered this to the immediate window:
0 0 0
0 1 2
2 0 0
Is it actually possible to make the For Each…Next construct correctly
populate the elements of an array, and if so, how please?