VBA array sort function?

后端 未结 13 2321
北荒
北荒 2020-11-22 05:28

I\'m looking for a decent sort implementation for arrays in VBA. A Quicksort would be preferred. Or any other sort algorithm other than bubble or merge would suffice.

<
13条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 05:55

    I think my code (tested) is more "educated", assuming the simpler the better.

    Option Base 1
    
    'Function to sort an array decscending
    Function SORT(Rango As Range) As Variant
        Dim check As Boolean
        check = True
        If IsNull(Rango) Then
            check = False
        End If
        If check Then
            Application.Volatile
            Dim x() As Variant, n As Double, m As Double, i As Double, j As Double, k As Double
            n = Rango.Rows.Count: m = Rango.Columns.Count: k = n * m
            ReDim x(n, m)
            For i = 1 To n Step 1
                For j = 1 To m Step 1
                    x(i, j) = Application.Large(Rango, k)
                    k = k - 1
                Next j
            Next i
            SORT = x
        Else
            Exit Function
        End If
    End Function
    

提交回复
热议问题