[英]faster method for sorting data smallest to largest using VBA
I use the following code to sort the data in worksheet cells,but for large amount of data,it takes a lot of time.我使用以下代码对工作表单元格中的数据进行排序,但是对于大量数据,需要花费很多时间。 Is there any other method to do it more faster?
有没有其他方法可以更快地做到这一点?
the used code is:使用的代码是:
Sub VBA_DataSorting()
Dim i As Integer
Dim j As Integer
Dim temp As Double ' must be double to contain fractional values
Dim rng As Range
Set rng = Range("A1").CurrentRegion 'Range of data to be sorted
For i = 1 To rng.count
For j = i + 1 To rng.count
If rng.Cells(j) < rng.Cells(i) Then ' sort smallest to largest
'swap numbers
temp = rng.Cells(i)
rng.Cells(i) = rng.Cells(j)
rng.Cells(j) = temp
End If
Next j
Next i
End Sub
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.