Dim A
Sub test()
Dim B, i, x
x = InputBox("列号:", "输入", 4)
A = Range("a1").CurrentRegion
ReDim B(2 To UBound(A))
'按第 x 列进行排序
For i = 2 To UBound(A)
B(i) = A(i, x)
Next i
Call BubbleSort(B)
[a21].Resize(UBound(A), UBound(A, 2)) = A
End Sub
'冒泡排序
Sub BubbleSort(Arr)
Dim l&, u&, i&, j&
l = LBound(Arr): u = UBound(Arr)
For i = l To u - 1
For j = u To i + 1 Step -1
If Arr(j) < Arr(j - 1) Then Swap Arr(j), Arr(j - 1): Swap2 j, j - 1 '升序
Next j
Next i
End Sub
'互换辅助数组中的两个值
Sub Swap(x, y)
Dim temp
temp = x: x = y: y = temp
End Sub
'互换实际数据中的两行
Sub Swap2(x, y)
Dim temp, j
For j = 1 To UBound(A, 2)
temp = A(x, j): A(x, j) = A(y, j): A(y, j) = temp
Next j
End Sub
Dim A
Sub test()
Dim B, i, x
x = InputBox("列号:", "输入", 4)
A = Range("a1").CurrentRegion
ReDim B(2 To UBound(A))
'按第 x 列进行排序
For i = 2 To UBound(A)
B(i) = A(i, x)
Next i
Call BubbleSort(B)
[a21].Resize(UBound(A), UBound(A, 2)) = A
End Sub
'冒泡排序
Sub BubbleSort(Arr)
Dim l&, u&, i&, j&
l = LBound(Arr): u = UBound(Arr)
For i = l To u - 1
For j = u To i + 1 Step -1
If Arr(j) < Arr(j - 1) Then Swap Arr(j), Arr(j - 1): Swap2 j, j - 1 '升序
Next j
Next i
End Sub
'互换辅助数组中的两个值
Sub Swap(x, y)
Dim temp
temp = x: x = y: y = temp
End Sub
'互换实际数据中的两行
Sub Swap2(x, y)
Dim temp, j
For j = 1 To UBound(A, 2)
temp = A(x, j): A(x, j) = A(y, j): A(y, j) = temp
Next j
End Sub