我想對從單元格A40開始,以A46結尾的7個值進行排序。我正在使用冒泡排序並創建兩個過程。但是,當我執行時,VBA告訴我下標超出範圍...有人能告訴我代碼中的問題在哪裏嗎?排序過程不起作用
Sub dort()
Dim plaga() As Variant
plaga = Worksheets("Sheet3").Range("A40:A46").Value
Call tri1(plaga)
Dim Destination As Range
Set Destination = Worksheets("Sheet3").Range("C40")
Destination.Resize(7, 1).Value = plaga
End Sub
Sub tri1(plaga As Variant)
Dim ligne_Deb As Long
Dim ligne_Fin As Long
ligne_Deb = LBound(plaga)
ligne_Fin = UBound(plaga)
Dim i As Long, j As Long
Dim tmp As Long
For i = ligne_Deb To ligne_Fin - 1
For j = ligne_Fin To i + 1 Step -1
If plaga(j) < plaga(j - 1) Then
tmp = plaga(j)
plaga(j) = plaga(j - 1)
plaga(j - 1) = tmp
End If
Next j
Next i
End Sub
你爲什麼這樣排序使用Excel內置'Sort'對象,而不是?請記住,你的程序效率低下 –