VBA Excel Variable Sorting on Multiple Keys/Orders

前端 未结 1 1619
自闭症患者
自闭症患者 2021-01-14 13:29

How can I sort on multiple columns (keys/orders) at the same time? When I run my below code it re-sorts the data when executing each line instead of one sort with multiple c

1条回答
  •  心在旅途
    2021-01-14 13:45

    Sub sdad()
        wsLast_Row = Cells(Rows.Count, 2).End(xlUp).Row
        With ActiveWorkbook.ActiveSheet.Sort
            .SortFields.Clear
            .SortFields.Add Key:=Range("A3:A" & wsLast_Row), Order:=xlAscending
            .SortFields.Add Key:=Range("B3:B" & wsLast_Row), Order:=xlAscending
            .SortFields.Add Key:=Range("C3:C" & wsLast_Row), Order:=xlAscending
            .SortFields.Add Key:=Range("D3:D" & wsLast_Row), Order:=xlAscending
            .SetRange Range("A3:BZ" & wsLast_Row)
            .Header = xlNo
            .Apply
        End With
    End Sub
    

    0 讨论(0)
提交回复
热议问题