Loop using string

in_d

New Member
Joined
Mar 3, 2009
Messages
47
I have just had a thought which would make my life a lot easier, but don't have a clue where to start..
Is it possible to apply the following principle for a loop to use a string instead of numbers? For example;

Code:
For i = 1 to 10
msgbox i
Next i

Could I do the same using letters of the alphabet? I don't know how it would work, but something like.

Code:
For i = a,b,c,d,e,f,g,h
msgbox i
Next i


Thanks in advance
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I have exhausted the technique of using a case select argument and assigning a different string based on the

Code:
Dim aStr as String
Dim i as Integer
 
For i = 1 To 27
Select Case i
    Case 1
        aStr = "CJ"
    Case 2
        aStr = "CJ"
    Case 3
        aStr = "CJ"
End Select
 
Msgbox aStr
Next i

I was just hoping there was a tidier way to do it..?
 
Upvote 0
? ? ? maybe something like this
Code:
Sub doit()
 
    Dim i As Integer
    Dim alpha As String
    Dim s As String
 
    Dim alpha_first As Integer
    Dim alpha_last as integer
 
    alpha_first = Asc("a")
    alpha_last = alpha_first + 25
 
    For i = alpha_first To alpha_last
        alpha = Chr$(i)
        s = s & alpha
    Next
 
    MsgBox s
 
End Sub
 
Upvote 0
you could also use:

Code:
for each str in array("A","B","C")
  debug.print str
next
 
Upvote 0
You could also do this:

Code:
Sub test()
Dim i As Long, t
t = Array("A", "B", "C", "D", "E", "F", "G")
For i = LBound(t) To UBound(t)
    MsgBox t(i)
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,207,011
Messages
6,076,143
Members
446,187
Latest member
LMill

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