自定义排序多列,具有两个自定义排序列表Excel VBA

时间:2015-08-17 09:51:59

标签: excel vba excel-vba sorting

我是Excel VBA的初学者。我很难搞清楚自定义排序。我记录了宏并根据我通过搜索互联网找到的内容进行了修改。我尝试过搜索,但大多数人只有一些排序参数和一列。我有超过20列,我想按如下方式对它们进行排序。有人可以检查我的错误是什么错误?谢谢你的帮助!

    masterws.Sort.SortFields.clear

    Application.DeleteCustomList Application.CustomListCount

    Application.AddCustomList ListArray:=ThisWorkbook.Worksheets("SortReference"). _
    Range("A1:A50")
    Application.AddCustomList ListArray:=ThisWorkbook.Worksheets("SortReference"). _
    Range("B1:B50")

    masterws.Autofilter.Sort.Sortfields.Add Key:=Range("A:A"), SortOn:xlSortOnValues, _
    Order:=Ascending, DataOption:xlSortnormal
    masterws.Autofilter.Sort.Sortfields.Add Key:=Range("B:B"), SortOn:xlSortOnValues, _
    Order:=Ascending, DataOption:xlSortnormal
    masterws.Autofilter.Sort.Sortfields.Add Key:=Range("C:C"), SortOn:xlSortOnValues, _
    Order:=Ascending, DataOption:xlSortnormal
    masterws.Autofilter.Sort.Sortfields.Add Key:=Range("D:D"), SortOn:xlSortOnValues, _
    Order:=Ascending, DataOption:xlSortnormal
    masterws.Autofilter.Sort.Sortfields.Add Key:=Range("E:E"), SortOn:xlSortOnValues, _
    Order:=Ascending, DataOption:xlSortnormal
    masterws.Autofilter.Sort.Sortfields.Add Key:=Range("F:F"), SortOn:xlSortOnValues, _
    Order:=Ascending, DataOption:xlSortnormal   
    masterws.Autofilter.Sort.Sortfields.Add Key:=Range("G:G"), SortOn:xlSortOnValues, _
    Order:=Ascending, DataOption:xlSortnormal
    masterws.Autofilter.Sort.Sortfields.Add Key:=Range("H:H"), SortOn:xlSortOnValues, _
    Order:=Ascending, DataOption:xlSortnormal

    masterws.Autofilter.Sort.Sortfields.Add Key=Range("J:J"), SortOn:xlSortOnValues, _
    Order:=Ascending, OrderCustom:=Application.CustomListCount + 1, DataOption:xlSortnormal

    masterws.Autofilter.Sort.Sortfields.Add Key=Range("I:I"), SortOn:xlSortOnValues, _
    Order:=Ascending, OrderCustom:=Application.CustomListCount + 2, DataOption:xlSortnormal

With masterws.Sort
    .Header=xlYes
    .MatchCase=False
    .Orientation=xlTopToBottom
    .SortMethod=xlPinYin
    .Apply
End With

0 个答案:

没有答案