使用VBA将数据从最小到最大排序的更快方法

时间:2017-04-26 21:20:31

标签: excel vba excel-vba

我使用以下代码对工作表单元格中的数据进行排序,但对于大量数据,需要花费大量时间,还有其他方法可以更快地完成吗?

使用的代码是:

FLOOR(newvalue * 1000)/1000

1 个答案:

答案 0 :(得分:0)

sub SortDataExample()

 'Building data to sort on the active sheet.
 Range("A1").Value = "Name"
 Range("A2").Value = "Bill"
 Range("A3").Value = "Rod"
 Range("A4").Value = "John"
 Range("A5").Value = "Paddy"
 Range("A6").Value = "Kelly"
 Range("A7").Value = "William"
 Range("A8").Value = "Janet"
 Range("A9").Value = "Florence"
 Range("A10").Value = "Albert"
 Range("A11").Value = "Mary"
 MsgBox "The list is out of order. Hit Ok to continue...", vbInformation

 'Selecting a cell within the range.
 Range("A2").Select

 'Applying sort.
 With ActiveWorkbook.Worksheets(ActiveSheet.Name).Sort
 .SortFields.Clear
 .SortFields.Add Key:=Range("A2:A11"), _
 SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
 .SetRange Range("A1:A11")
 .Header = xlYes
 .MatchCase = False
 .Orientation = xlTopToBottom
 .SortMethod = xlPinYin
 .Apply
 End With
 MsgBox "Sort complete.", vbInformation

End Sub