Excel UDF - 对单元格内用逗号分隔的数字和文本进行排序
Posted
技术标签:
【中文标题】Excel UDF - 对单元格内用逗号分隔的数字和文本进行排序【英文标题】:Excel UDF - sort numbers and text separated by comma within a cell 【发布时间】:2019-04-02 20:17:09 【问题描述】:我有一列数据,每个单元格中都有数字和文本,用逗号分隔。我在另一个论坛中找到了一个 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
【问题讨论】:
【参考方案1】:您的代码将数组的内容视为文本,因此这就是值按原样排序的原因。
不幸的是,考虑将数组的类型从String
更改为Long
或Double
并不那么简单,因为您的NA
(或任何其他String
)值...
这个解决方案不是很优雅,我不喜欢看到那么长的函数,但它确实有效。
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
【讨论】:
你好,查尔斯。谢谢您的回复。我试过你的建议,它带有“无效的 Redim”错误。不幸的是,我的编码技能太低了,无法理解或解决这个问题。 @ManojKumar,我已经更正了我的代码,现在它应该可以工作了。随时通知我! 你好,查尔斯。这非常有效。非常感谢您花时间帮助我。 没问题。感谢您指定我的回答解决了您的问题。以上是关于Excel UDF - 对单元格内用逗号分隔的数字和文本进行排序的主要内容,如果未能解决你的问题,请参考以下文章