我使用宏按一列中的数据对表进行排序:
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/