For Each "Item" in Array?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
I was wondering if it is possible to use a For Each-Next construct to cycle through each element of an array, as one does with object collections? In other words, can an array and its elements be treated as objects, or is the only option to use a nested For-Next construct with the appropriate array subscripts?

Thanks for any help.

Kelvin
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I was wondering if it is possible to use a For Each-Next construct to cycle through each element of an array, as one does with object collections? In other words, can an array and its elements be treated as objects, or is the only option to use a nested For-Next construct with the appropriate array subscripts?

What kind of array are we talking about... range? data type? number of dimensions?
 
Upvote 0
General VBA variant array with multiple but unknown/unspecified number of dimensions.
 
Last edited:
Upvote 0
General VBA variant array with multiple but unknown/unspecified number of dimensions.

With V declared as Variant, you should be able to do this...

Code:
  For Each V In Arr
    Debug.Print V
  Next
It appears to "loop" through the first dimension first, then the second dimension and so on.
 
Upvote 0
OK, many thanks for this info - very helpful! However I'm working with ParamArrays with variable arrays, so I may have no choice but to use the less efficient For Each-Next approach...
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top