For Each loops with Arrays

evanair

New Member
Joined
Aug 17, 2005
Messages
3
I'd like to use a for each loop to step through the elements of a single-dimensional array. I know I can accomplish this using the Ubound function, because I've done it this way for years.

But I've always wanted to be able to structure it something like this, but I just don't know the right syntax: :cry:

Dim aSingle(10) as string
Dim myString$

... array loaded with strings, then:

For each myString$ in aSingle.???
...
Next myString$


So would anything work in place of the 3 question marks (???) to make this code work? Thanks for looking.
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,048
Office Version
  1. 365
Platform
  1. Windows
Have you tried it without anything for the ??? and no dot(.) qualifier.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
You will also need to declare myString as type Variant

Code:
Sub test()
Dim mystring As Variant, asingle(10) As String, x As Integer
For x = 0 To 10
    asingle(x) = Chr(65 + x)
Next x

For Each mystring In asingle
    MsgBox mystring
Next
End Sub
 

evanair

New Member
Joined
Aug 17, 2005
Messages
3
works

Thanks for the tip about using a variant data type. This works just as I expected.

It feels like reaching finally reaching an itch you've had for about a year.

-em
 

Watch MrExcel Video

Forum statistics

Threads
1,118,231
Messages
5,571,028
Members
412,355
Latest member
BasicExelHelp
Top