VBA array sort function?

后端 未结 13 1952
北荒
北荒 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 06:02

    This is what I use to sort in memory - it can easily be expanded to sort an array.

    Sub sortlist()
    
        Dim xarr As Variant
        Dim yarr As Variant
        Dim zarr As Variant
    
        xarr = Sheets("sheet").Range("sing col range")
        ReDim yarr(1 To UBound(xarr), 1 To 1)
        ReDim zarr(1 To UBound(xarr), 1 To 1)
    
        For n = 1 To UBound(xarr)
            zarr(n, 1) = 1
        Next n
    
        For n = 1 To UBound(xarr) - 1
            y = zarr(n, 1)
            For a = n + 1 To UBound(xarr)
                If xarr(n, 1) > xarr(a, 1) Then
                    y = y + 1
                Else
                    zarr(a, 1) = zarr(a, 1) + 1
                End If
            Next a
            yarr(y, 1) = xarr(n, 1)
        Next n
    
        y = zarr(UBound(xarr), 1)
        yarr(y, 1) = xarr(UBound(xarr), 1)
    
        yrng = "A1:A" & UBound(yarr)
        Sheets("sheet").Range(yrng) = yarr
    
    End Sub
    

提交回复
热议问题