Loop through an array and get a value

JOAO12

New Member
Joined
Feb 1, 2013
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Hello everyone,

I am trying to run the below code in order to loop through a few arrays and get the first value in each array, but, unfortunately it is not working.

The message box should display the values: "1", "4" and "7".

Thanks.


VBA Code:
Dim allArrays As Variant, myArray1 As Variant, myArray2 As Variant, myArray3 As Variant, element As Variant

allArrays = Array("myArray1", "myArray2", "myArray3")
myArray1 = Array("1", "2", "3")
myArray2 = Array("4", "5", "6")
myArray3 = Array("7", "8", "9")

For Each element In allArrays
MsgBox element(1)
Next element
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try it like
VBA Code:
Dim allArrays As Variant, myArray1 As Variant, myArray2 As Variant, myArray3 As Variant, element As Variant

myArray1 = Array("1", "2", "3")
myArray2 = Array("4", "5", "6")
myArray3 = Array("7", "8", "9")
allArrays = Array(myArray1, myArray2, myArray3)

For Each element In allArrays
MsgBox element(1)
Next element
 
Upvote 0
Try it like
VBA Code:
Dim allArrays As Variant, myArray1 As Variant, myArray2 As Variant, myArray3 As Variant, element As Variant

myArray1 = Array("1", "2", "3")
myArray2 = Array("4", "5", "6")
myArray3 = Array("7", "8", "9")
allArrays = Array(myArray1, myArray2, myArray3)

For Each element In allArrays
MsgBox element(1)
Next element
shouldn't be element(0) or element(lbound(element))
 
Upvote 0
If the Op is running Option Base 1 then the modified code will return the 1st value in each array.
I decided to fix the 1st part of the problem & wait see what the OP said.
 
Upvote 0
Fluff and MoshiM,

Thank you very much.

Fluff, your code is running properly.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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