Excel formula to take string value from cell and s

2019-09-15 04:28发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-15 05:11

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)

enter image description here

查看更多
登录 后发表回答