简体   繁体   中英

Excel conversion of text containing ranges--numeric to alpha-numeric

I would like to convert a range of numbers (and single digits) from a number-only format to alpha-numeric format. Entire statement is in a single, excel cell and would like the converted version to be in a neighboring cell.

As an example:

Assuming 1-24=B1-B24

Assuming 25-48=C1-C24

INPUT—

 screen 1-3,5,7-9,11-30,32-37,39-40,41,44-46

DESIRED OUTPUT (all acceptable)

   screen B1-B3,B5,B7-B9,B11-C6,C8-C13,C15-C16,C17,C20-C22

OR

 screen B1-B3,B5,B7-B9,B11-B24,C1-C6,C8-C13,C15-C16,C17,C20-C22

OR

 screen B1-B3,B5,B7-B9,B11-B24

   screen C1-C6,C8-C13,C15-C16,C17,C20-C22

Using excel functions is proving quite cumbersome so excel macro would be better. I've looked for examples of requested conversion but haven't found anything.

Any help is greatly appreciated.

Cheers,

Bob

Hey here is a solution that i tested out. Not sure if "screen" needs to be in the string or not. Let me know and I will tweak it if that's the case.

Its a user defined function. So drop this vba in a module and then go to a worksheet and type in "=AlphaConvert(" + the cell reference.

Assumption here is that only one cell will be referenced at a time.

Last this could easily be converted to a sub routine and probably run a bit faster than the function.

Public Function AlphaConvert(TargetCell As Range)
    Dim v       As Long
    Dim vArr()  As String
    Dim i       As Long
    Dim iArr()  As String
    Dim a       As String

    vArr = Split(TargetCell.Value, ",")

    For v = LBound(vArr) To UBound(vArr)
        If InStr(vArr(v), "-") > 0 Then
            iArr = Split(vArr(v), "-")

            For i = LBound(iArr) To UBound(iArr)
                If i = LBound(iArr) Then
                    a = AlphaCode(iArr(i))
                Else
                    a = a & "-" & AlphaCode(iArr(i))
                End If
            Next i

            vArr(v) = a
        Else
            vArr(v) = AlphaCode(vArr(v))
        End If

        If v = LBound(vArr) Then
            AlphaConvert = vArr(v)
        Else
            AlphaConvert = AlphaConvert & "," & vArr(v)
        End If
    Next v
End Function

Private Function AlphaCode(Nbr As Variant)
    Select Case Nbr
        Case 1 To 24
            AlphaCode = "B" & Nbr
        Case Else
            AlphaCode = "C" & Nbr - 24
    End Select
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM