In VBA, Excel allows sorting values using the CustomOrder parameter to choose the sequence items are ordered. Unfortunately, the sequence of items is delimited by commas and one of my sort items contains commas. For example, I want to sort the data in the first column by the categories in the second column. The "Air, Land, or Sea" category contains commas.
Data1 Aerospace Data2 Cyberspace Data3 Cyberspace Data4 Air, Land, or Sea Data5 Aerospace Data6 Air, Land, or Sea Data7 Cyberspace
If you record a VBA macro, the code created looks like this:
MyWorksheet.Sort.SortFields.Add Key:=Range( _
"B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, _
CustomOrder:= "Cyberspace,Air,Land,or Sea,Aerospace", _
DataOption:=xlSortNormal
MyWorksheet.Sort.Apply
So, the custom sort order should be "Cyberspace" then "Air, Land, or Sea", then "Aerospace". However, the second category is treated as three categories because of the commas. The rows with "Air, Land, or Sea" get sorted to the bottom because Excel doesn't find a custom sort match for them. Is there a way to get CustomOrder to work with a category that contains embedded commas?
I tried putting double quotes around the category and I tried replacing the delimiter commas with semicolons (in the hope Excel would accept a semicolon instead of a comma). Neither worked.