2

我正在尝试对从单元格 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
4

1 回答 1

2

谁能告诉我代码中的问题在哪里,好吗?

plaga = Worksheets("Sheet3").Range("A40:A46").Value

当您将范围存储在数组中时,它不是一维数组。

将您的代码更改为

plaga(j,1)

注意,1. 将其融入任何地方。

例如

For i = ligne_Deb To ligne_Fin - 1
    For j = ligne_Fin To i + 1 Step -1
        If plaga(j, 1) < plaga(j - 1, 1) Then
            tmp = plaga(j, 1)
            plaga(j, 1) = plaga(j - 1, 1)
            plaga(j - 1, 1) = tmp
        End If
    Next j
Next i

有趣的阅​​读

于 2013-04-12T10:21:25.690 回答