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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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
You always have a choice.
 
Upvote 0

Forum statistics

Threads
1,215,080
Messages
6,123,013
Members
449,093
Latest member
ikke

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