VBA自定义排序

EXCEL自定义排序最多只能有255个值,超过了就不能用自定义序列了,使用以下方法

求助excelhome论坛得出的答案

Sub 自定义排序()
Application.ScreenUpdating = False
'排序标准
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
Dim rng, i%
rng = Sheets("查补排序").[a1].CurrentRegion
For i = 2 To UBound(rng)             '不含标题
        d(rng(i, 1)) = i        '序号
Next
'数据源
Dim arr, brr
arr = [a1].CurrentRegion
ReDim brr(1 To UBound(arr, 2))
For i = 5 To UBound(arr, 2)       '不含标题
    If d.Exists(arr(1, i)) Then
        brr(i) = d(arr(1, i))      '序号
    Else
        brr(i) = "不存在"
    End If
Next
Set d = Nothing
Rows(1).Insert
[a1].Resize(1, UBound(brr)) = brr
[e1].Resize(UBound(arr) + 1, UBound(arr, 2)).Sort key1:=[e1], Orientation:=xlLeftToRight, order1:=xlAscending '按行排序,降序
Rows(1).Delete
Application.ScreenUpdating = True
MsgBox "排序完成"
End Sub

 

    原文作者:小ih熊
    原文地址: https://blog.csdn.net/pengxuanlan/article/details/102676023
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞