Function/Formula 对逗号分隔的列表进行排序 alphabetically/ascending 顺序
Function/Formula that sorts a comma seperated list alphabetically/ascending order
例如,我有一个单元格的值为:
1133,1131,1141,1142,1143,1151,1132,1181
如何按升序排列?
1131,1132,1133,1141,1142,1143,1151,1181
这样做是为了处理很多不同的事情,所以希望找到一个公式或 vba 函数驱动的解决方案。
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
考虑:
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
注意:
按照您标题中的要求,该函数执行字母排序而不是数字排序。
例如,我有一个单元格的值为:
1133,1131,1141,1142,1143,1151,1132,1181
如何按升序排列?
1131,1132,1133,1141,1142,1143,1151,1181
这样做是为了处理很多不同的事情,所以希望找到一个公式或 vba 函数驱动的解决方案。
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
考虑:
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
注意:
按照您标题中的要求,该函数执行字母排序而不是数字排序。