繁体   English   中英

Excel UDF-在单元格中对数字和文本进行排序,并以逗号分隔

[英]Excel UDF - sort numbers and text separated by comma within a cell

我有一列数据,每个单元格中都有数字和文本,用逗号分隔。 我在另一个论坛(请参见下面的代码)中找到了UDF,但它做得并不好。 例如:

原始单元格:

84,86,NA,268,277,400,411,42,120,244,346

UDF结果:

120、244、268、277、346、400、411、42、84、86,不适用

所需结果:

42,84,86,120,244,268,277,346,400,411,不适用

我想知道是否有人可以帮助我修复此代码。 非常感谢 最好的祝福马诺伊

查看我在另一个论坛中找到的UDF代码

Function StrSort(ByVal sInp As String, _
                  Optional bDescending As Boolean = False) As String
    ' sorts a comma-delimited string
    Dim asSS()  As String    ' substring array
    Dim sSS     As String    ' temp string for exchange
    Dim n       As Long
    Dim i       As Long
    Dim j       As Long

    asSS = Split(sInp, ",")
    n = UBound(asSS)

    For i = 0 To n
        asSS(i) = Trim(asSS(i))
    Next

    If n < 1 Then
        StrSort = sInp
    Else
        For i = 0 To n - 1
            For j = i + 1 To n
                If (asSS(j) < asSS(i)) Xor bDescending Then
                    sSS = asSS(i)
                    asSS(i) = asSS(j)
                    asSS(j) = sSS
                End If
            Next j
        Next i
        StrSort = Join(asSS, ", ")
    End If
End Function

您的代码将数组的内容视为文本,因此这就是为什么值按原样排序的原因。

不幸的是,考虑到您的NA (或任何其他String )值,仅考虑将数组的类型从String更改为LongDouble并不是那么简单...

这个解决方案不是很优雅,我不希望看到功能这么长,但是可以用。

Public Function StrSort(ByVal sInp As String, _
                  Optional bDescending As Boolean = False) As String
    ' sorts a comma-delimited string
    Dim asSS()  As String    ' substring array
    Dim sSS     As String    ' temp string for exchange
    Dim n       As Long
    Dim i       As Long
    Dim j       As Long

    asSS = Split(sInp, ",")
    n = UBound(asSS)

    'First, we are gonna sort Numeric values from every other type of value.
    'The numeric values are going to be stored in an array containing only numeric values
    Dim TemporaryNumberArray() As Double
    For i = 0 To n
        If IsNumeric(Trim(asSS(i))) Then
            On Error Resume Next
            If IsError(UBound(TemporaryNumberArray)) Then
                ReDim TemporaryNumberArray(0 To 0)
            Else
                ReDim Preserve TemporaryNumberArray(0 To UBound(TemporaryNumberArray) + 1)
            End If
            On Error GoTo 0
            TemporaryNumberArray(UBound(TemporaryNumberArray)) = asSS(i)
        End If

    Next


    n = UBound(TemporaryNumberArray)

    'Now, we are going to sort the numbers array.
    If n < 1 Then
        StrSort = sInp
    Else
        For i = 0 To n - 1
            For j = i + 1 To n
                If (TemporaryNumberArray(j) < TemporaryNumberArray(i)) Xor bDescending Then
                    sSS = TemporaryNumberArray(i)
                    TemporaryNumberArray(i) = TemporaryNumberArray(j)
                    TemporaryNumberArray(j) = sSS
                End If
            Next j
        Next i

        'Now, we are building the return string that contains the numbers in order
        StrSort = CStr(TemporaryNumberArray(0))
        For i = 1 To n
            StrSort = StrSort & ", " & CStr(TemporaryNumberArray(i))
        Next
    End If

    'Finally, we are going to append the non-numeric values at the end, in the same order as they appear in the input string
    If n < UBound(asSS) Then
        For i = 0 To UBound(asSS)
            If Not IsNumeric(asSS(i)) Then
                StrSort = StrSort & ", " & asSS(i)
            End If
        Next
    End If
End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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