Excel formula to take string value from cell and sort its characters in alphabetical order

独自空忆成欢 提交于 2019-12-25 04:29:30

问题


Can you please help me to make Excel formula that takes string value from cell and sorts its characters in alphabetical order?

Ex.

original cell value: 'BACR' sorted characters cell: 'ABCR'


回答1:


This UDF will sort numbers and Text character by character:

Function sortletter(rng As Range)
    If rng.Count > 1 Then Exit Function
    Dim srtArr() As String
    Dim i&, j&, k&
    ReDim srtArr(1 To Len(rng))
    srtArr(1) = Mid(rng, 1, 1)
    For i = 2 To UBound(srtArr)
        For j = 1 To UBound(srtArr)
            If srtArr(j) = "" Then
                srtArr(j) = Mid(rng, i, 1)
                Exit For
            ElseIf IIf(Asc(Mid(rng, i, 1)) > 96, Asc(Mid(rng, i, 1)) - 32, Asc(Mid(rng, i, 1))) <= IIf(Asc(srtArr(j)) > 96, Asc(srtArr(j)) - 32, Asc(srtArr(j))) Then
                For k = UBound(srtArr) To j + 1 Step -1
                    srtArr(k) = srtArr(k - 1)
                Next k
                srtArr(j) = Mid(rng, i, 1)
                Exit For
            End If
        Next j
    Next i
    sortletter = Join(srtArr, "")
End Function

Put this is a module attached to the workbook, NOT in the worksheet or ThisWorkbook code.

Then it can be called like any other formula

=sortletter(A1)



来源:https://stackoverflow.com/questions/39086737/excel-formula-to-take-string-value-from-cell-and-sort-its-characters-in-alphabet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!