VBA Type Mismatch on CustomOrder

后端 未结 4 749
情歌与酒
情歌与酒 2020-12-11 05:12

I have this code which works great:

Sub NewSortTest()
    ActiveWorkbook.Worksheets(\"Sheet1\").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets(\"Sheet1\         


        
4条回答
  •  旧时难觅i
    2020-12-11 05:46

    I know this is an old thread but figured my approach seems worth sharing.

    I stored the SortField as a variable then set the CustomOrder property as per below, it seems the complier will take the delimited string and convert it to whatever it requires. FYI loNewReport is a ListObject

    Dim sField As SortField
    Set sField = loNewReport.Sort.SortFields.Add(loNewReport.ListColumns("Branch").DataBodyRange, xlSortOnValues, xlAscending)
    sField.CustomOrder = "QLD, NSW, VIC"
    loNewReport.Sort.Apply
    Set sField = Nothing
    

提交回复
热议问题