Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Convert numeric code with VBA

Posted on 2015-01-06
Medium Priority
100 Views
I need to change a numeric value into a string based on a predefined code. Currently, I convert the numeric value into string. Check and replace the numeric value into corresponding data. Just wonder whether there is any better way to handle this ? Tks

21 -> AX
eg.
1 = X
2 = A
3 = G
0
Question by:AXISHK
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 49

Expert Comment

ID: 40534957
Usage: MsgBox ConvertToString(21)

``````Function ConvertToString(strValue As String) As String

Const Letters = "XAG"
Dim lngDigit As Long

For lngDigit = 1 To Len(strValue)
ConvertToString = ConvertToString & Mid\$(Letters, Mid\$(strValue, lngDigit, 1), 1)
Next
End Function
``````
0

LVL 2

Assisted Solution

Pratik Makwana earned 400 total points
ID: 40534975
You can use below code for single column..
Sub ReplaceFunctions()
Dim dat As Variant
Dim rng As Range
Dim i As Long

Set rng = Range("A1:G27" & ActiveSheet.UsedRange.Rows.Count)
dat = rng
For i = LBound(dat, 1) To UBound(dat, 1)
If dat(i, 1) = "1" Then
dat(i, 1) = "X"
ElseIf dat(i, 1) = "2" Then
dat(i, 1) = "A"
ElseIf dat(i, 1) = "3" Then
dat(i, 1) = "G"
End If
Next
rng = dat
End Sub
0

LVL 15

Assisted Solution

ChloesDad earned 800 total points
ID: 40535021
Rather than searching each time for the correct letter, as in the above example, you could create an array of letters and then print the appropriate letter

``````Function ConvertToString(strValue As String) As String

Dim Letters(10)

Letters(0) = "Z"
Letters(1)="X"
Letters(2)="A"
Letters(3)="G"
etc

ConvertToString = ConvertToString & Letters(Mid\$(strValue, lngDigit, 1))

End Function
``````
0

Author Comment

ID: 40543767
No sure how to recursively call the function, supposed Pricecode() array has been loaded with 0-9, Tks

MsgBox ConvertToString("512")

Private Function ConvertToString(strValue As String) As String

Dim lngDigit As Long

For lngDigit = 1 To Len(strValue)
ConvertToString = ConvertToString(PriceCode(Mid\$(strValue, lngDigit, 1)))
Next

End Function
0

LVL 49

Accepted Solution

Martin Liss earned 800 total points
ID: 40544128
This assumes that 4 to 9 are HIJKL and M.
``````Function ConvertToString(strValue As String) As String

Const Letters = "XAGHIJKLM"
Dim lngDigit As Long

For lngDigit = 1 To Len(strValue)
ConvertToString = ConvertToString & Mid\$(Letters, Mid\$(strValue, lngDigit, 1), 1)
Next
End Function
``````
0

LVL 15

Expert Comment

ID: 40547555
Why would you need to recursively call this?

The input is a number and the output is a string.

In a function, the function name is also by default a local variable of the return type, so you don't need the Return x at the end of the function.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
###### Suggested Courses
Course of the Month12 days, 3 hours left to enroll

#### 636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.