使用VBA在Excel中排序多列

问题描述 投票:0回答:2

我必须在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

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

excel excel-vba vba
2个回答
2
投票

正如user3514930所说:您在.Apply之前缺少End With


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

正如其他用户所说。

© www.soinside.com 2019 - 2024. All rights reserved.