Convert numeric characters to alphabetic characters

前端 未结 5 1349
日久生厌
日久生厌 2021-01-02 16:12

I am trying to get I/O as follows:

Input : 123490
Output : BCDEJA

Logic is simple:

if
strarr(i)=0,1,2

相关标签:
5条回答
  • 2021-01-02 16:39

    Make use of the ASCII character numbers (...?) and adjust them by the digits you are converting. A capitol A is ASCII 0×41 or 65 dec.

    Function num_alpha(str As String)
        Dim sTMP As String, d As Long
    
        For d = 1 To Len(str)
            sTMP = sTMP & Chr(65 + Mid(str, d, 1))
        Next d
    
        num_alpha = sTMP
    
    End Function
    

    Use like any native worksheet function. In D18 as,

    =num_alpha(B18)
    

          

    0 讨论(0)
  • 2021-01-02 16:47

    As Jeeped said you can use the Chr function to convert a number to a letter, using ASCII.

    on another note, when working with a single variable which can have multiple values, instead of using so much if's I would suggest using a case select model, use strarr(i) as the controller it would simplify your code and would be a lot more readable.

    Also, instead of writing the different values to cells, I would have used a temporary variable to store the aggregated value, less hassle for you and a bit faster as you don't read/write to the sheet instead you're just working in the background

    0 讨论(0)
  • 2021-01-02 16:50

    This should get you started:

    Public Function ConvertValue(iInput As Integer) As String
        ConvertValue = Chr(65 + iInput)
    End Function
    

    Please note that value of '65'stands for capital A, lowercase letters start from '97

    0 讨论(0)
  • 2021-01-02 16:51

    I liked Jeeped's answer.

    The version below works on this with some tweaks to play with the speed:

    • Mid as a LHS operator (as concatenation in VBA is slower)
    • use of Mid$ and ChrW$

    On my testing reduced run-time by ~40% (see edits below)

     Function NumChr(strIn As String) As String
            Dim strTemp As String
            Dim lngChar As Long
    
            For lngChar = 1 To Len(strIn)
                Mid$(strIn, lngChar, 1) = ChrW$(65 + Mid$(strIn, lngChar, 1))
            Next
            NumChr = strTemp
        End Function
    

    EDIT: Add tests

    1. 3.21 seconds for initial code
    2. 1.98 seconds for second code

    high level reconciliation

    • Using Mid$ in first code rather than Mid took code from 3.21 to 2.77 seconds.
    • Using ChrW$ rather than Chr took it from 2.77 to 2.43 seconds.
    • Using Mid$ on the LHS took it to 1.98 seconds

    prior code

    Function num_alpha(str As String)
        Dim sTMP As String, d As Long
    
        For d = 1 To Len(str)
            sTMP = sTMP & Chr(65 + Mid(str, d, 1))
        Next d
    
        num_alpha = sTMP
    
    End Function
    

    new code

    Function NumChr(strIn As String) As String
        Dim lngChar As Long
    
        For lngChar = 1 To Len(strIn)
            Mid$(strIn, lngChar, 1) = ChrW$(65 + Mid$(strIn, lngChar, 1))
        Next
        NumChr = strIn
    End Function
    

    test timing

    Sub Main()
    Call Test
    Call Test2
    End Sub
    
    Sub Test()
    Dim dbTimer As Double
    dbTimer = Timer()
    For i = 1 To 1000000
        s = num_alpha("123490")
    Next
    Debug.Print Timer() - dbTimer
    End Sub
    
    Sub Test2()
    Dim dbTimer As Double
    dbTimer = Timer()
    For i = 1 To 1000000
        s = NumChr("123490")
    Next
    Debug.Print Timer() - dbTimer
    End Sub
    
    0 讨论(0)
  • 2021-01-02 16:55
    =CHAR(64 + 1)
    will Give "A"
    =CHAR(64 + 2)
    will Give "B" 
    =CHAR(64 + 3)
    will Give "C" 
    
    so on.....
    
    0 讨论(0)
提交回复
热议问题