+ Reply to Thread
Results 1 to 6 of 6

Step through the alphabet with ForNext loop

Hybrid View

  1. #1
    Valued Forum Contributor jwright650's Avatar
    Join Date
    12-10-2010
    Location
    Va, USA
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    606

    Step through the alphabet with ForNext loop

    How do you go about looping through the alphabet Using a For/Next loop?....I can step through using numbers, but can't seem to declare the correct variables and steps to do the alphabet...
    Tried this...but it's wrong. What I was trying to do was get 1A, 2A, 3A through 5A.....down column A



     
    Sub NestedForLoops()
        Dim r      As Long
        Dim alpha      As String
        alpha = "A"
        For r = 1 To 5 Step 1
            For alpha = "A" To "E" Step 1
                Cells(r, 1).Select
                Cells(r, 1).Value = CStr(r) & CStr(alpha)
            Next alpha
        Next r
    End Sub
    Last edited by jwright650; 04-11-2011 at 01:47 PM.
    Life is like a roll of toilet paper. The closer it gets to the end, the faster it goes.
    John Wright

  2. #2
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Step through the alphabet with ForNext loop

    With the code #'s maybe.

    CODE("A") is 65, CODE("B") is 66, etc

  3. #3
    Valued Forum Contributor jwright650's Avatar
    Join Date
    12-10-2010
    Location
    Va, USA
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    606

    Re: Step through the alphabet with ForNext loop

    Quote Originally Posted by Cutter View Post
    With the code #'s maybe.

    CODE("A") is 65, CODE("B") is 66, etc
    I thought about using the Char#'s but wasn't sure. I was able to get dates to advance a day at a time....just have never been able to get the alphabet to do that.

  4. #4
    Valued Forum Contributor jwright650's Avatar
    Join Date
    12-10-2010
    Location
    Va, USA
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    606

    Re: Step through the alphabet with ForNext loop

    @Cutter:
    The Chr#'s work...now I just need to figure out the for/next nesting to get it to do what I needed. Thanks for the nudge in the right direction.

  5. #5
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Step through the alphabet with ForNext loop

    I'm not good at VBA so I can't really help you but something like

    Chr(64 + r)

    maybe?

  6. #6
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Step through the alphabet with ForNext loop

    I think this is what you want - adjusted what you had

    Sub NestedForLoops()
        Dim r      As Long
        For r = 1 To 5 Step 1
                Cells(r, 1).Select
                Cells(r, 1).Value = CStr(r) & CStr(Chr(64 + r))
        Next r
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1