當前位置:成語大全網 - 新華字典 - VBA 數組排序

VBA 數組排序

最簡單的排序,須借助excel的函數:

Sub SmallSort()

Dim a(-1 To 3), i, b()

For i = LBound(a) To UBound(a)

a(i) = Int(Rnd * 10) '賦值給a()

Next

Rows("5:6").Delete

[a5].Resize(1, UBound(a) - LBound(a) + 1) = a

'下面是排序方法,結果放在b()內

ReDim b(1 To UBound(a) - LBound(a) + 1)

For i = 1 To UBound(b)

b(i) = Application.WorksheetFunction.Small(a, i) 'Small為從小到大,large為從大到小

Next

[a6].Resize(1, UBound(b)) = b

End Sub