Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA从最小到最大排序数据的快速方法_Vba_Excel - Fatal编程技术网

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

使用VBA从最小到最大排序数据的快速方法,vba,excel,Vba,Excel,我使用以下代码对工作表单元格中的数据进行排序,但对于大量数据,这需要花费大量时间。有没有其他方法可以更快地完成 使用的代码是: 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 da

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

使用的代码是:

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
子VBA_数据排序()
作为整数的Dim i
作为整数的Dim j
Dim temp As Double'必须为双精度才能包含分数值
变暗rng As范围
设置rng=范围(“A1”)。CurrentRegion要排序的数据范围
对于i=1到rng.count
对于j=i+1至rng.count
如果rng.Cells(j)
为什么不使用工具栏上的内置排序功能?@Scott Craner我想使用VBA宏,而不是Manullayth然后使用
range.sort
方法。如果有内置函数,则范围的气泡排序是不明智的。您所做的操作可能适用于阵列。我个人仍然体验到在临时工作表中写入一个范围,对该范围进行排序并回写到数组也会更快。
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