Solved

Convert numeric code with VBA

Posted on 2015-01-06
6
76 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 45

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 45

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now