VB code to convert positive integer N into an alphanumeric string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stephie3883
    New Member
    • Feb 2013
    • 6

    VB code to convert positive integer N into an alphanumeric string

    Convert a positive integer with N digits into an alphanumeric string where A = 0, B = 1, …, J = 9. Let N be less than or equal to 1000000.

    N=776655

    So far I have:

    Code:
    Sub code_num()
    Dim N As integer
    Dim Alpha As String
    N=776655
    I am new to VBA and i'm not really sure on where to even start with this problem....
    Last edited by Rabbit; Feb 28 '13, 04:55 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    There are a few options, one way is to use the replace function to replace each number from 0 to 9 with its letter equivalent.

    Comment

    • stephie3883
      New Member
      • Feb 2013
      • 6

      #3
      ok so i would set A=0, B=1 etc.
      which means for N=776655, my end result should be HHGGFF right?
      Which command assigns each number a letter?

      Comment

      • Mikkeee
        New Member
        • Feb 2013
        • 94

        #4
        I haven't coded in VBA in a while so I'm not 100% on the syntax but I believe this will work for you. What I did to get the correct alpha value was add 65 (ASCII decimal code for A) to your number which will get me the correct decimal value and brought that back to a character.

        Code:
           Dim N As Integer
           Dim Alpha As String
           N = 776655
           Alpha = CStr(N)
           For i As Integer = 1 To Len(Alpha)
               Mid(Alpha, i, 1) = Chr(CInt(Mid(Alpha, i, 1)) + 65)
           Next
           ' Alpha variable will now be HHGGFF
        Ascii Table: http://www.asciitable.com/

        Comment

        Working...