I'm trying to create a For Loop that only selects some values but can't work out the Syntax, or if it's even possible?
I want it to be like
Dim i As Integer
For i = 1,3,8,15 Then
Do something
Next i
Any Ideas?
You can do following:
Dim i as Variant
Dim iArray as Variant
iArray=Array(1,3,8,15)
For Each i In iArray
'Do Something
Next i
Cheers
This solution combines elements from the array solution above with the ease of use that I inferred was wanted by the OP. The array method requires looping through the array elements and then converting the array element back into a variable. This method skips 2 bits. Firstly, it skips the complex step logic of stepping through array elements. Secondly, it skips the step of converting an array element back into a usable variable.
Dim number_list as collection
set number_list = new collection
number_list.add 1
number_list.add 3
number_list.add 8
number_list.add 15
for each number in number_list
'Do something with number
Next number
Collection?This is even a simplification of the routine of Kedimir:
Original question:
Dim i As Integer
For i = 1,3,8,15 Then
' Do something
Next i
Solution:
Dim i As Variant
For Each i In Array(1, 3, 8, 15)
' Do something
Next i
For your information: the Variant is needed for the For Each loop, don't try to modify it into an integer, or the For Each loop won't be accepted.
I don't think that a more resembling solution is possible :-)