Reputation: 99
I guess this is a easy one but I can't figure it out.
I have an array with multiple items, as it follows:
slds = Array(3, 15, 27, 39, 51, 87, 74, 89, 11, 45, 57, 24)
I want to do a For
loop using just some items the array, defined by the position, for example, the first 4 elements, but I don't know the correct syntax for it. Something like
For slds(0) to slds(3) 'do some...
Any ideas?
Upvotes: 1
Views: 62
Reputation: 55682
Or you could just Redim
the array to capture the first x values.
For loop retained in case you still want to loop.
Sub a()
Dim slds
Dim lngCnt As Long
slds = Array(3, 15, 27, 39, 51, 87, 74, 89, 11, 45, 57, 24)
ReDim Preserve slds(1 To 3)
For lngCnt = 1 To UBound(slds)
Debug.Print slds(lngCnt)
Next
End Sub
Upvotes: 2
Reputation: 13122
Assuming a typical zero-based array in VBA:
For i = 0 To 3
'do stuff
Debug.Print slds(i)
Next
Here you are indicating you want index 0 to 3, or the first 4 elements.
If you can't be certain of zero-based, you'd use
For i = lBound(slds) to Lbound(slds) + 3
as mentioned by Scott Craner
Upvotes: 2