VB icon

AlphaConvert

Email
Submitted on: 1/7/2015 12:23:00 PM
By: Matt Marooney (from psc cd)  
Level: Advanced
User Rating: By 3 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 397
 
     Converts two character strings of A-Z or AA-ZZ into integer equivalents. Example: User inputs "A", returns 1, user inputs AE, returns 110 and so on.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: AlphaConvert
' Description:Converts two character strings of A-Z or AA-ZZ into integer equivalents. Example: User inputs "A", returns 1, user inputs AE, returns 110 and so on.
' By: Matt Marooney (from psc cd)
'
' Inputs:Two character string variable is passed to this function
'
' Returns:Returns Integer equiv of the string
'**************************************

' User inputs a string of 2 characters, uppercase or lowercase.
'Function returns the combined integer value of the string (ex. A = 1, B=2...
'AA = 27, AB = 28...ect.)
Function GetNumber(UserInput As String) As Integer
Dim UpperCaseArray(1, 26) As String
Dim LowerCaseArray(1, 26) As String
Dim UpperCaseString As String
Dim LowerCaseString As String
Dim FirstNum As Integer
Dim SecondNum As Integer
UpperCaseString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LowerCaseString = "abcdefghijklmnopqrstuvwxyz"
'Assign string characters to array cells
For x = 1 To Len(UpperCaseString)
UpperCaseArray(1, x) = Mid(UpperCaseString, x, 1)
LowerCaseArray(1, x) = Mid(LowerCaseString, x, 1)
Next x
If Len(UserInput) = 1 Then ' check for single character input
For y = 1 To Len(UpperCaseString)
'If the input from the user is A-Z or a-z the Function returns 1-26
If Mid(UserInput, 1, 1) = UpperCaseArray(1, y) Then
GetNumber = y
End If
If Mid(UserInput, 1, 1) = LowerCaseArray(1, y) Then
GetNumber = y
End If
Next y
Else
'If User Input has two characters...
'Check first character...store numerical value in FirstNum
For z = 1 To Len(UpperCaseString)
If Mid(UserInput, 1, 1) = UpperCaseArray(1, z) Then
FirstNum = z
End If
If Mid(UserInput, 1, 1) = LowerCaseArray(1, z) Then
FirstNum = z
End If
Next z
'Check second character
'Store numerical value in SecondNum
For w = 1 To Len(UpperCaseString)
If Mid(UserInput, 2, 1) = UpperCaseArray(1, w) Then
SecondNum = w
End If
If Mid(UserInput, 2, 1) = LowerCaseArray(1, w) Then
SecondNum = w
End If
Next w
'Algorithm for adding the values for the first character to that
'of the second character to determine which set of 26 the user
'selected.
'i.e. if user enters "AA" then this loop determines that the first
'character is equal to one. the loop returns 26 + 1, or 27. So, the
'value of user input of "AA" is 27. And so on and so forth...
'If the value entered is "BA", the algorithm returns 52 + 1, or 53
'This loop will return the values for up to "IZ"
'To extend to ZZ, merely change number of iterations in this loop to 26
For V = 1 To 9
If FirstNum = V Then
GetNumber = ((26 * V) + SecondNum)
End If
Next V
End If
End Function


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.