ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop from a to z (https://www.excelbanter.com/excel-programming/396376-loop-z.html)

Madiya

Loop from a to z
 
I need to loop from a to z.
say
for i=a to z
......
next

How can I do it?

Regards,
Madiya


Vergel Adriano

Loop from a to z
 
one way is something like this:

Sub test()
Dim i As Integer
For i = Asc("a") To Asc("z")
Debug.Print Chr(i)
Next i
End Sub


--
Hope that helps.

Vergel Adriano


"Madiya" wrote:

I need to loop from a to z.
say
for i=a to z
......
next

How can I do it?

Regards,
Madiya



Rick Rothstein \(MVP - VB\)

Loop from a to z
 
I need to loop from a to z.
say
for i=a to z
.....
next

How can I do it?


Here is one way to do it directly as you wrote it...

Enum Letters
a = 97: b: c: d: e: f: g: h: i: j: k: l: m
n = 110: o: p: q: r: s: t: u: v: w: x: y: z
End Enum

Sub test()
Dim X As Variant
For X = a To z
Debug.Print Chr(X)
Next
End Sub

Note that the Enum can be one single line long, like this...

Enum Letters
a = 97: b: c: d: e: f: g: h: i: j: k: l: m: n: o: p: q: r: s: t: u: v: w:
x: y: z
End Enum

but I didn't do it because I suspect the line would wrap in most
newsreaders. If it did, and you copied it that way, the first letter on the
wrapped line would be taken as a Line Label by VB; so, I figured it was
better to break it up for clarity during copying... make it into one line
after you place it into your code.

Note... while I used lower case letters in the Enum and in the For-Next
loop, the casing it not significant... upper case letters work exactly the
same as lower case within the code... the output, however, will still be
lower case because the Enum starts its assignment at 97 (the ASCII code for
lower case 'a').

Rick



All times are GMT +1. The time now is 05:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com