Excel 排序顺序 - 特殊字符不排在前面

标签 excel vba sorting columnsorting

我使用宏按一列中的数据对表进行排序:

ActiveWorkbook.Worksheets("sheet").Sort.SortFields.Add Key:=Range(sortRange), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

有没有办法让这段代码按以下顺序排序:首先是 0-9,然后是 A-Z,然后是特殊字符(至少有 • 和 +,我喜欢将它们排在排序顺序的最后)?

最佳答案

好吧,这听起来是一项有趣的任务,所以我在另一个工作表中使用不同的列表尝试了 Vityata 的方法。

Sub crazySort()

Dim ws As Worksheet
Dim ws2 As Worksheet
Dim lastRow As Long
Dim yourcolumnindex, letters, numbers, others As Long
Dim i As Long

Set ws = Worksheets("sheet")
'This is the sheet for our temp lists, rename accordingly
Set ws2 = Worksheets("tempsheet")
columnsCount = x
i = 1
letters = 1
others = 1
numbers = 1

With ws
For j = 1 to columnsCount
    'loop through all the cells in your column
    'change yourcolumnindex accordingly
    Do While .Cells(i, j) <> ""
        'check for the ASCII-code of the first character in every list

        Select Case Asc(Left(.Cells(i, j), 1))
            Case 65 To 90, 97 To 122
                'if it's a letter, put it in column 1
                ws2.Cells(letters, 1) = .Cells(i, j)
                letters = letters + 1
            Case 48 To 57
                'if it's a cipher, put it in column 2
                ws2.Cells(numbers, 2) = .Cells(i, j)
                numbers = numbers + 1
            Case Else
                'is it something else, put it in column 3
                ws2.Cells(others, 3) = .Cells(i, j)
                others = others + 1
        End Select
        i = i + 1
    Loop
Next
End With

End Sub

这部分仅包含拆分列表,但从现在开始,它只是排序和复制/粘贴回来。

祝你玩得开心。

关于Excel 排序顺序 - 特殊字符不排在前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38635315/

相关文章:

vba - Excel VBA宏代码不停地插入无限行

vba - VBA Excel 类模块中的子属性

c# - 字典列表排序

excel - VBA:何时使用函数与将代码放在主子中

vba - 外部地址不固定

string - Excel中删除除第一个单词之外的所有以特定字母开头的单词的第一个字符

c - 从数组中获取最大排序子数组的算法

java - 如何对链表进行排序?

java - 为什么用 Java 编写 Excel 时会发生这种情况

excel - 在VBA中使用VLookup引用另一个Excel文件