3

I'm having a small problem with a for each loop iterating through an array of integers. I have something like this:

Dim arr(3) as Integer
Dim vari as variant

for each vari in arr
  debug.print vari
next var

While it does return the value correctly I would also need a way to refer to the index number of a given item in the array(whether it is arr(1),(2) etc). How do I do this with a for each loop? I know how to do it using a for x = y to z loop, but I'd rather keep it as a for each loop.

2
  • Why would you "rather keep it as a for each loop"? Your question is basically "how can I do X without using the tool I already know about that is designed to do exactly what I want"? Commented Jan 3, 2017 at 0:19
  • I was hoping to be able to keep it as a for each loop to make it easier to maintain, as the number of items in the array will most likely keep changing. It did not occur to me to use the UBound LBound to do this. Commented Jan 3, 2017 at 9:37

3 Answers 3

10

You need to use a traditional for loop if you want easy access to the element index. Try this...

Sub Test2()

    Dim arr(3) As Integer
    Dim vari As Variant
    arr(0) = 5: arr(1) = 4: arr(2) = 3: arr(3) = 2

    Dim lIdx As Long
    For lIdx = LBound(arr) To UBound(arr) '* though you have defined the bounds to be 3 above !!  anyway...
        vari = arr(lIdx)
        Debug.Print vari, lIdx
    Next lIdx
'    For Each vari In arr
'      Debug.Print vari
'    Next Var
End Sub

There is no way to get the index number from a For Each loop. If you want evidence then here is the documentation for the interface IEnumVARIANT that underlines For Each in VB6/VBA https://msdn.microsoft.com/en-us/library/windows/desktop/ms221053(v=vs.85).aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the link. Also 'LBound to UBound' worked like a charm and eliminates my need for 'for each'.
1

If you really insist on using For Each, then you need to keep track of the index using a counter variable, i.e. the variable idx in the below code:

Dim arr(1 to 3) as Integer '<-- 1 to 3 if you want your array index start with 1 instead of zero
Dim vari as variant
Dim idx as long: idx = LBound(arr)

For Each vari In arr
    debug.print "The item at index " & idx & " is: " & vari
    idx = idx + 1
Next vari

Comments

-2

Here try this modifying it first.

Sub forEachArray()

Dim element As Variant
Dim animals(0 To 5) As String
'We have created an array that can hold 6 elements

animals(0) = "Dog"
animals(1) = "Cat"
animals(2) = "Bird"
animals(3) = "Buffalo"
animals(4) = "Snake"
animals(5) = "Duck-billed Platypus"
'We fill each element of the array


For Each element In animals
'animals consists of 6 "elements"

    Debug.Print element
    'printing to the immediate window

Next

End Sub

1 Comment

Reason for downvote: This answer misses the main point of the question - getting the index inside the For Each. Improvement suggestion: show how to print the index, not the value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.