按字母顺序/升序对逗号分隔列表进行排序的函数/公式

时间:2016-09-29 04:05:57

标签: excel vba sorting formula

示例,我拥有的一个单元格具有值:

1133,1131,1141,1142,1143,1151,1132,1181

如何按升序进行此操作?

1131,1132,1133,1141,1142,1143,1151,1181

这样做可以处理很多不同的事情,所以希望找到一个公式或vba函数驱动的解决方案。

2 个答案:

答案 0 :(得分:2)

Option Explicit

Function sortingHat(str As String, _
                    Optional delim As String = ",")
    Dim a As Long, b As Long, arr As Variant, tmp As Variant

    'split the string into an array
    arr = Split(str, delim)

    'sort the array
    For a = LBound(arr) To UBound(arr) - 1
        For b = a + 1 To UBound(arr)
            If arr(b) < arr(a) Then
                tmp = arr(a)
                arr(a) = arr(b)
                arr(b) = tmp
            End If
        Next b
    Next a

    'join the array into a string
    sortingHat = Join(arr, delim)
End Function

sortingHat

答案 1 :(得分:0)

考虑:

Function MySort(txt As String, Sep As String) As String

    Dim a

    With CreateObject("System.Collections.ArrayList")
        For Each a In Split(txt, Sep)
            .Add Trim$(CStr(a))
        Next
        .Sort
        MySort = Join(.ToArray, Sep)
    End With
End Function

enter image description here

注意:

根据标题中的要求,该函数执行字母排序,而不是数字排序。