Solved

Convert numeric code with VBA

Posted on 2015-01-06
6
78 Views
Last Modified: 2015-01-13
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
Comment
Question by:AXISHK
6 Comments
 
LVL 46

Expert Comment

by:Martin Liss
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

Open in new window

0
 
LVL 2

Assisted Solution

by:Pratik Makwana
Pratik Makwana earned 100 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

by:ChloesDad
ChloesDad earned 200 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

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:AXISHK
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 46

Accepted Solution

by:
Martin Liss earned 200 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

Open in new window

0
 
LVL 15

Expert Comment

by:ChloesDad
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

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

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

Join & Ask a Question