0

I have this VBA code that loops through an array (in this case, supplier_reports()) and it works fine. I was wondering if anyone know if there is a way to get the element number when working with the value in the array:

For Each x In supplier_reports

Workbooks.Open (x)

element_number = x

next x

In this case element number just becomes the value of x, not it's element number in the array.

Thanks

1
  • You need to have another variable as a counter that increments at the end of each loop. Commented Sep 24, 2015 at 14:56

1 Answer 1

3

You can use a For Loop instead of For Each.

Eg.

For I = LBound(supplier_reports) To UBound(supplier_reports)
 Workbooks.Open (supplier_reports(I))
 element_number = I
Next
Sign up to request clarification or add additional context in comments.

2 Comments

Good idea, I wasn't crazy about using a different structure, but this isn't a bad idea.
Thomas, In case of For Each as well, index is maintained internally. So no performance / memory loss also will be there.

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.