【问题标题】:Arrange excel value in alphabetical order按字母顺序排列excel值
【发布时间】:2017-07-24 15:48:28
【问题描述】:

美好的一天!请问有没有办法按字母顺序排列单元格值?

A 列示例

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

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

结果应该是这样的:

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

【问题讨论】:

  • 一种方法是遍历 A 列,使用split function 将每个单元格按, 拆分,从而生成一个数组。然后对该数组进行排序并使用join function 将数组加入字符串并将其写回单元格。如果您在任何地方遇到困难,请返回并发布您的代码。

标签: excel excel-formula vba


【解决方案1】:

你可以使用这个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多