Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用VBA排序-多级排序_Excel_Vba_Sorting - Fatal编程技术网

Excel 使用VBA排序-多级排序

Excel 使用VBA排序-多级排序,excel,vba,sorting,Excel,Vba,Sorting,我有一个自定义列表在Excel中只有一个用于排序的自定义列表 我首先按:列A按日期升序排序 H列字母H、A、B自定义列表 G列日期递减 但是得到了一个错误的结果 正确排序: 试试看 Sub test() Dim LRow As Long Dim rngDB As Range Dim v As Integer Dim Ws As Worksheet Application.AddCustomList ListArray:=Array("H", "A",

我有一个自定义列表在Excel中只有一个用于排序的自定义列表 我首先按:列A按日期升序排序 H列字母H、A、B自定义列表 G列日期递减

但是得到了一个错误的结果

正确排序: 试试看

Sub test()
    Dim LRow As Long
    Dim rngDB As Range
    Dim v As Integer
    Dim Ws As Worksheet

    Application.AddCustomList ListArray:=Array("H", "A", "B")
    v = Application.CustomListCount

    Set Ws = ActiveWorkbook.Worksheets("Basis")

    With Ws
        LRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rngDB = .Range("a1", .Range("h" & LRow))
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range("A1"), Order:=xlAscending
        .Sort.SortFields.Add Key:=.Range("h1"), Order:=xlDescending, CustomOrder:=v
        .Sort.SortFields.Add Key:=.Range("g1"), Order:=xlDescending
        With .Sort
            .SetRange rngDB
            .Header = xlYes
            .Orientation = xlTopToBottom
            .Apply
        End With
    End With
    Application.DeleteCustomList v
End Sub

嗯-您可能需要再次添加正确的排序图片。我认为我正确地编辑了这个问题,但看起来两个截图是相同的。为什么CustomListCount+1?尝试以下常规模式:在调用sort之前添加排序字段这是不同的结果这是不同的结果-这是什么?它和什么不一样?请尽量在您的回答中更详细一点,否则很难提供任何帮助。感谢您的支持Ted,排序应如中所示picture@CanUlucan-你把名字弄混了。这个答案来自李迪,而不是弗朗西斯,你在《你好,李迪》中也说了Ted而不是Tim,谢谢,干得好!
Sub test()
    Dim LRow As Long
    Dim rngDB As Range
    Dim v As Integer
    Dim Ws As Worksheet

    Application.AddCustomList ListArray:=Array("H", "A", "B")
    v = Application.CustomListCount

    Set Ws = ActiveWorkbook.Worksheets("Basis")

    With Ws
        LRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rngDB = .Range("a1", .Range("h" & LRow))
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range("A1"), Order:=xlAscending
        .Sort.SortFields.Add Key:=.Range("h1"), Order:=xlDescending, CustomOrder:=v
        .Sort.SortFields.Add Key:=.Range("g1"), Order:=xlDescending
        With .Sort
            .SetRange rngDB
            .Header = xlYes
            .Orientation = xlTopToBottom
            .Apply
        End With
    End With
    Application.DeleteCustomList v
End Sub