簡體   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