按字母顺序排列excel值

时间:2017-07-24 06:37:09

标签: excel excel-vba excel-formula vba

美好的一天!我想问一下是否有办法按字母顺序排列单元格值?

示例列A

A,M
A,Q
Q,A
M,A

我想按字母顺序排列单个单元格的值。没有对列进行排序。

结果应该是这样的:

A,M
A,Q
A,Q
A,M

1 个答案:

答案 0 :(得分:2)

您可以使用此VBA代码,该代码使用this answer进行排序:

Dim col As Variant
Dim list As Variant
Dim i As Long
Dim part as Variant

' Create a list that can be used for sorting
Set list = CreateObject("System.Collections.ArrayList")

' Get cell contents into an array and loop over each value
col = ActiveSheet.UsedRange.Columns(1)
For i = 1 To UBound(col)
    ' Extract the comma-separated values with Split and add them to the list
    list.Clear
    For Each part In Split(col(i, 1), ",")
        list.Add part
    Next
    ' Now use the handy Sort method on that list
    list.Sort
    ' Join the result back to the comma-separated format, and put it in the array
    col(i, 1) = Join(list.ToArray(), ",")
Next
' Put the modified array contents back in the sheet where they came from
ActiveSheet.UsedRange.Columns(1) = col
相关问题