使用VBA在Excel中对多个列进行排序

时间:2014-08-05 13:59:37

标签: excel excel-vba vba

我必须在Excel中对其值中的单独列进行排序。我制定了以下程序:

                With calcCalculations

With .Sort

.SortFields.Add Key:=Range("BR2:BR5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("BV2:BV5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("CA2:CA5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("CD2:CD5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

         .SetRange Range("BR:CD")
         .Header = xlYes
         .MatchCase = True
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin


        End With

End with

该过程不会出错,但它不会对我的数据进行排序。我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

正如用户3514930所说:您在.Apply

之前错过了End With

答案 1 :(得分:0)

                With calcCalculations

With .Sort

.SortFields.Add Key:=Range("BR2:BR5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("BV2:BV5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("CA2:CA5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("CD2:CD5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

         .SetRange Range("BR:CD")
         .Header = xlYes
         .MatchCase = True
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
        End With

End with

如其他用户所说。