简体   繁体   English

Excel 2016 - 按升序对单元格中的值进行排序

[英]Excel 2016 - Sort value in cell in ascending order

I currently have a cell with some values, separated by commas.我目前有一个包含一些值的单元格,用逗号分隔。 I wish to sort them in ascending order.我希望按升序对它们进行排序。

Sample Input (Value in a single cell):样本输入(单个单元格中的值):

样本输入图像

Sample Output (Value in a single cell):示例 Output(单个单元格中的值):

样本输出图像

I have seen many answers when it comes to sorting rows and columns, but I can't seem to sort the values in a single cell in ascending order.在对行和列进行排序时,我已经看到了很多答案,但我似乎无法按升序对单个单元格中的值进行排序。 Is it possible to sort the values in a single cell in ascending order?是否可以按升序对单个单元格中的值进行排序? Or is there a workaround for this?或者有解决方法吗?

Some explanation/documentation would be appreciated as I'm a beginner at VBA.一些解释/文档将不胜感激,因为我是 VBA 的初学者。 Thank you for your help.谢谢您的帮助。

Please try to split data in a single cell by comma, then sorting it and combine all of them together?请尝试用逗号分隔单个单元格中的数据,然后对其进行排序并将所有这些数据组合在一起?

The comments provide you with numerous ways to sort an array.注释为您提供了多种对数组进行排序的方法。 Rightly or wrongly, I've provided my own flavour on an old VBA topic along with the extended piece of outputting the string in a delimited format.无论对错,我都提供了我自己对旧的 VBA 主题的看法,以及以分隔格式输出字符串的扩展部分。 Take it or leave it...!要么接受,要么离开它...!

You should be able to refer to the below function directly from any given cell like you would any built-in function in Excel.您应该能够直接从任何给定的单元格中参考下面的 function,就像在 Excel 中的任何内置 function 一样。

Public Function SplitAndSortAscending(ByVal strText As String, ByVal strDelimiter As String) As String
    Dim arrData() As String, arrNewData() As String, i As Long, x As Long, y As Long
    
    arrData = Split(strText, strDelimiter)
    ReDim arrNewData(UBound(arrData))
    
    For i = 0 To UBound(arrData)
        For x = 0 To UBound(arrNewData)
            If arrData(i) < arrNewData(x) Or arrNewData(x) = "" Then
                For y = UBound(arrNewData) To x + 1 Step -1
                    arrNewData(y) = arrNewData(y - 1)
                Next
                
                arrNewData(x) = arrData(i)
                Exit For
            End If
        Next
    Next
    
    For i = 0 To UBound(arrNewData)
        SplitAndSortAscending = SplitAndSortAscending & strDelimiter & arrNewData(i)
    Next
    
    SplitAndSortAscending = Mid(SplitAndSortAscending, Len(strDelimiter) + 1)
End Function

If you have O365, you can use something like the below to achieve the same sort of thing.如果你有 O365,你可以使用类似下面的东西来实现同样的事情。 Take note, my implementation will take 1.0 and format it as a whole number, ie it will come out as 1 .请注意,我的实现将采用1.0并将其格式化为整数,即输出为1

=TEXTJOIN(",",TRUE,SORT(FILTERXML("<r><v>" & SUBSTITUTE(A1,",","</v><v>") & "</v></r>","//v")))

The assumption is that the example you provided is in cell A1 .假设您提供的示例位于单元格A1中。

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

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