# For Each Loop on the 2nd Dimension Array Only

1. ## For Each Loop on the 2nd Dimension Array Only

How would one go about using a For Each Loop to the second dimension only? It'd be easy to do it with UBound and LBound with a For Loop but I'm struggling in getting this to work with a For Each Loop.

[CODE]
i = 1
j = 1
For Each j in Arr (i, j)
Debug.Print Arr(i, j)
Next j
[CODE]

Obviously it's not recognising the upper limit in the 2nd dimension. Surely it's possible to use a For Each Loop on the 2nd dimension in an array?

2. ## Re: For Each Loop on the 2nd Dimension Array Only

values in cells.jpg

``Please Login or Register  to view this content.``

3. ## Re: For Each Loop on the 2nd Dimension Array Only

Thanks Kev, to clarify you're saying that it's not possible with a For Each Loop?

4. ## Re: For Each Loop on the 2nd Dimension Array Only

This also works for a horizontal array:

``Please Login or Register  to view this content.``

5. ## Re: For Each Loop on the 2nd Dimension Array Only

you're saying that it's not possible with a For Each Loop?

Dimension 1 upper bound
- UBound(myArray, 1)

Dimension 2 upper bound
- UBound(myArray, 2)

I hope that clarifies it for you

6. ## Re: For Each Loop on the 2nd Dimension Array Only

Thanks guys, my query is - is it not possible to loop the 2nd dimension with a For Each Loop?

7. ## Re: For Each Loop on the 2nd Dimension Array Only

Originally Posted by kev_

Dimension 1 upper bound
- UBound(myArray, 1)

Dimension 2 upper bound
- UBound(myArray, 2)
Thanks Kev, as I mentioned in my opening post I was well aware of attaching the UBound and LBound functions. I want to know if it's possible using a For Each Loop as stipulated in this tutorial; but it works on the whole array.

My query is can a For Each Loop be specified on the second dimension only?

8. ## Re: For Each Loop on the 2nd Dimension Array Only

You are misunderstanding what you are seeing

EDIT
Apologies - our posts are crossing
- I now understand that you ONLY want to consider FOR EACH...

9. ## Re: For Each Loop on the 2nd Dimension Array Only

Use my example of 10 values - which values do you want?

10. ## Re: For Each Loop on the 2nd Dimension Array Only

To use For each loop and the 2nd element, then you want every other element in the array - like this:
``Please Login or Register  to view this content.``
or with a message box
``Please Login or Register  to view this content.``
ForEach.jpg

11. Hi !

Originally Posted by Dal123
My query is can a For Each Loop be specified on the second dimension only?
At beginner level just using INDEX worksheet function :

PHP Code:
```     For Each V In Application.Index(Arr, , 2)         Debug.Print V     Next  ```
Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

12. ## Re: For Each Loop on the 2nd Dimension Array Only

With my example array this returns 10,20,30,40,50 which is all the values of the 2nd element
``Please Login or Register  to view this content.``

13. ## Re: For Each Loop on the 2nd Dimension Array Only

Just try my original code ‼ Without Transpose …

14. ## Re: For Each Loop on the 2nd Dimension Array Only

Yes that returns 2 and 20

Ok you can return the values whichever way is up,
... but I am obviously missing the point

We have returned the values with both For i = .. and For Each...
... what is now missing?

15. ## Re: For Each Loop on the 2nd Dimension Array Only

Obviously Dal123 must crystal clear explain his need with a complete code (as we can't guess how the array is loaded !)
like he should do when creating a new thread, from the beginning until expected result …

16. ## Re: For Each Loop on the 2nd Dimension Array Only

Apologies if I'm being unclear I was trying to not bloat the thread with a superfluous thread. I'm sorry for wasting all of your time with lacking information I'm not sure if I should start a new clean thread tomorrow as I've made a right mess of this one, sorry guys.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1