By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

VB code to convert positive integer N into an alphanumeric string

P: 6
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.


So far I have:

Expand|Select|Wrap|Line Numbers
  1. Sub code_num()
  2. Dim N As integer
  3. Dim Alpha As String
  4. N=776655
I am new to VBA and i'm not really sure on where to even start with this problem....
Feb 28 '13 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 10K+
P: 12,383
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.
Feb 28 '13 #2

P: 6
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?
Feb 28 '13 #3

P: 94
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.

Expand|Select|Wrap|Line Numbers
  1.    Dim N As Integer
  2.    Dim Alpha As String
  3.    N = 776655
  4.    Alpha = CStr(N)
  5.    For i As Integer = 1 To Len(Alpha)
  6.        Mid(Alpha, i, 1) = Chr(CInt(Mid(Alpha, i, 1)) + 65)
  7.    Next
  8.    ' Alpha variable will now be HHGGFF
Ascii Table:
Feb 28 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.