Results 1 to 8 of 8

Thread: [RESOLVED] How to increment letters like a to b then to c ...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Resolved [RESOLVED] How to increment letters like a to b then to c ...

    Hi all .could any one show me how i can increment letters for example if i have letter text5=a next time i call the function the value be text5=b and so on untill i reach z and it jumps back to a ! I have done similer think for integers as shown below but want to do it for letters .Hope some one show me how i can
    increment letters.Thanks


    Text5.Text = CStr(CLng(Text5.Text) + 1)

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to increment letters like a to b then to c ...

    This is an example in which, for demonstration purposes, I am using two textboxes.

    It will take the number in text1 and find its equivalent in the alphabet, and display the letter in text2.

    You can take my example, and modify it for your use
    vb Code:
    1. Private Sub Command1_Click()
    2. Const ALPHA_BET As String = "abcdefghijklmnopqrstuvwxyz"
    3. Dim intLetter As Integer
    4. Dim intNum As Integer
    5. intNum = CInt(Text1.Text)
    6.  
    7.     For intLetter = 1 To Len(ALPHA_BET)
    8.         If intLetter = intNum Then
    9.            Text2.Text = UCase(Mid(ALPHA_BET, intLetter, 1))
    10.            Exit For
    11.         End If
    12.     Next
    13.  
    14. End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to increment letters like a to b then to c ...

    thanks hack . i go this solution but it goes beyond letter z . Is there away to stop it at Z and start from a all over again ?

    Code:
    Private Sub Command1_Click()
       If Option7.Value = True Then
           Text14.Text = Chr(Asc(Text14.Text) + 1)
       End If
    
    End sub
    By looking at you solution how i can restart from a to z all over again when i reach letter z. i see that integer keeps incrementing ?is there a

    Code:
    Private Sub Command1_Click()
       
          Const ALPHA_BET As String = "abcdefghijklmnopqrstuvwxyz"
       
          Dim intLetter As Integer
       
          Dim intNum As Integer
    
       Text1.Text = CStr(CLng(Text1.Text) + 1)
    
    
          intNum = CInt(Text1.Text)
       
       
              For intLetter = 1 To Len(ALPHA_BET)
       
                  If intLetter = intNum Then
       
                     Text2.Text = LCase(Mid(ALPHA_BET, intLetter, 1))
      
                     Exit For
      
                  End If
      
              Next
      End Sub


    My goal is to change value of textbox14 which initially start with letter "a" and keep incrementing it till "z" and the all over again ...until user stop clicking the function!
    Last edited by tony007; May 10th, 2011 at 12:52 PM.

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to increment letters like a to b then to c ...

    Quote Originally Posted by tony007 View Post
    Hi all .could any one show me how i can increment letters for example if i have letter text5=a next time i call the function the value be text5=b and so on untill i reach z and it jumps back to a ! I have done similer think for integers as shown below but want to do it for letters .Hope some one show me how i can
    increment letters.Thanks


    Text5.Text = CStr(CLng(Text5.Text) + 1)

    If Text5.Text = "z" Then Text5.Text = Chr(Asc("a") - 1)
    Text5.Text = Chr(Asc(Text5.Text) + 1)


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: How to increment letters like a to b then to c ...

    Quote Originally Posted by tony007 View Post
    thanks hack . i go this solution but it goes beyond letter z . Is there away to stop it at Z and start from a all over again ?

    Code:
    Private Sub Command1_Click()
       If Option7.Value = True Then
           Text14.Text = Chr(Asc(Text14.Text) + 1)
       End If
    End sub
    Tony

    That is the approach that came to mind as I started reading
    thru the thread.

    To deal with your "stopping at z" issue, it might be helpful
    to keep this in mind:
    • Asc("a") = 97
    • Asc("z") = 122

    You could add a "test" .... if you are at 122, reset to 97.
    I bet that you can take it from here.

    BTW, it is case-sensitive. To wit,
    • Asc("A") = 65
    • Asc("Z") = 90


    EDIT: jmsrickland snuck his reply in while I was typing -- he stole my idea

    Spoo
    Last edited by Spoo; May 10th, 2011 at 01:04 PM.

  6. #6
    Lively Member S0LARIS's Avatar
    Join Date
    Apr 2011
    Posts
    121

    Re: How to increment letters like a to b then to c ...

    My version

    vb Code:
    1. Dim iChar As Integer
    2.  
    3. For iChar = Asc("a") To Asc("z")
    4.  
    5. MsgBox Chr(iChar)
    6. If iChar = Asc("z") Then iChar = Asc("a") - 1
    7.  
    8. Next iChar
    If my post was helpful to you, then express your gratitude using Rate this Post.
    To understand recursion, you must first understand recursion.

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to increment letters like a to b then to c ...

    Yes, as you were typing I looked behind the window and saw what you were typing


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to increment letters like a to b then to c ...

    Many Many thanks for all of you . jmsrickland i think your solution was shortest one !
    Code:
    Private Sub Command1_Click()
    If Option7.Value = True Then
    'for incrementing letters till z
    If Text5.Text = "z" Then Text5.Text = Chr(Asc("a") - 1)
    Text5.Text = Chr(Asc(Text5.Text) + 1)
    End If
    
    End sub
    Hack i manged to stop your first solution like this:

    Code:
       
    
    Private Sub Command1_Click()
    
       Const ALPHA_BET As String = "abcdefghijklmnopqrstuvwxyz"
       
          Dim intLetter As Integer
       
          Dim intNum As Integer
          
    For i = 0 To Text1.Text = 27
       Text1.Text = CStr(CLng(Text1.Text) + 1)
       
       If Text1.Text = 27 Then
       Text1.Text = 1
       End If
       
    Next
          intNum = CInt(Text1.Text)
       
       
              For intLetter = 1 To Len(ALPHA_BET)
       
                  If intLetter = intNum Then
       
                     Text2.Text = LCase(Mid(ALPHA_BET, intLetter, 1))
      
                     Exit For
      
                  End If
      
              Next
    End sub

Posting Permissions

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



Click Here to Expand Forum to Full Width