使用自定义顺序和包含逗号的值编写 Excel VBA 排序代码

发布于 2024-11-09 09:48:32 字数 766 浏览 0 评论 0原文

在 VBA 中,Excel 允许使用 CustomOrder 参数对值进行排序,以选择排序的序列项。不幸的是,项目序列是用逗号分隔的,并且我的排序项目之一包含逗号。例如,我想按第二列中的类别对第一列中的数据进行排序。 “空中、陆地或海上”类别包含逗号。

Data1 航空航天
Data2网络空间
Data3网络空间
Data4 空中、陆地或海上
Data5 航空航天
Data6 空中、陆地或海上
Data7 网络空间

如果您录制 VBA 宏,则创建的代码如下所示:

MyWorksheet.Sort.SortFields.Add Key:=Range( _
    "B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, _
    CustomOrder:= "Cyberspace,Air,Land,or Sea,Aerospace", _
    DataOption:=xlSortNormal  
MyWorksheet.Sort.Apply

因此,自定义排序顺序应为“网络空间”,然后是“空中、陆地或海洋”,然后是“航空航天”。然而,由于逗号的原因,第二个类别被视为三个类别。包含“Air、Land 或 Sea”的行会排序到底部,因为 Excel 找不到它们的自定义排序匹配项。 有没有办法让 CustomOrder 处理包含嵌入逗号的类别?

我尝试在类别周围加上双引号,并尝试用分号替换分隔符逗号(希望 Excel 能够接受分号而不是逗号)。两者都不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短暂陪伴 2024-11-16 09:48:32

似乎缺少Apply。您可以添加

MyWorksheet.Sort.Apply

您的自定义订单正在按我的示例中的方式工作吗?

编辑根据OP更新的问题进行更新

将宏编辑为以下内容 - 使用 OrderCustom 参数的数组。

Dim oWorksheet As Worksheet
Set oWorksheet = ActiveWorkbook.Worksheets("Sheet1")
Dim oRangeSort As Range
Dim oRangeKey As Range

' one range that includes all colums do sort
Set oRangeSort = oWorksheet.Range("A1:B9")
' start of column with keys to sort
Set oRangeKey = oWorksheet.Range("B1")

' custom sort order
Dim sCustomList(1 To 3) As String
sCustomList(1) = "Cyberspace"
sCustomList(2) = "Aerospace"
sCustomList(3) = "Air, Land, or Sea"

Application.AddCustomList ListArray:=sCustomList
' use this if you want a list on the spreadsheet to sort by
' Application.AddCustomList ListArray:=Range("D1:D3")

oWorksheet.Sort.SortFields.Clear
oRangeSort.Sort Key1:=oRangeKey, Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

' clean up
Application.DeleteCustomList Application.CustomListCount
Set oWorksheet = Nothing

It seems to be missing the Apply. Can you Add

MyWorksheet.Sort.Apply

The custom order you have is working as is in my sample.

EDIT Updated based on OP updated question

Edit the macro to the following - using an array for the OrderCustom parameter.

Dim oWorksheet As Worksheet
Set oWorksheet = ActiveWorkbook.Worksheets("Sheet1")
Dim oRangeSort As Range
Dim oRangeKey As Range

' one range that includes all colums do sort
Set oRangeSort = oWorksheet.Range("A1:B9")
' start of column with keys to sort
Set oRangeKey = oWorksheet.Range("B1")

' custom sort order
Dim sCustomList(1 To 3) As String
sCustomList(1) = "Cyberspace"
sCustomList(2) = "Aerospace"
sCustomList(3) = "Air, Land, or Sea"

Application.AddCustomList ListArray:=sCustomList
' use this if you want a list on the spreadsheet to sort by
' Application.AddCustomList ListArray:=Range("D1:D3")

oWorksheet.Sort.SortFields.Clear
oRangeSort.Sort Key1:=oRangeKey, Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

' clean up
Application.DeleteCustomList Application.CustomListCount
Set oWorksheet = Nothing
羁拥 2024-11-16 09:48:32

好的...根据更新的描述,您要排序的内容旁边的列的公式怎么样。

因此,如果“空中、陆地或海洋”位于 B1 列中,则 C1 中将显示以下内容:

=SUBSTITUTE(B1,",","|")

然后您可以像这样进行自定义排序:

MyWorksheet.Sort.SortFields.Add Key:=Range( _

        "B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        CustomOrder:= "Cyberspace,Air|Land|or Sea,Aerospace", _
        DataOption:=xlSortNormal  
    MyWorksheet.Sort.Apply

确保适当调整范围。

OK...based on the updated description, how about a formula for the column next to what you're sorting.

So, if "Air, Land, or Sea" is in column B1, then C1 would have this:

=SUBSTITUTE(B1,",","|")

Then you could do your custom sort like so:

MyWorksheet.Sort.SortFields.Add Key:=Range( _

        "B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        CustomOrder:= "Cyberspace,Air|Land|or Sea,Aerospace", _
        DataOption:=xlSortNormal  
    MyWorksheet.Sort.Apply

Make sure to adjust the range appropriately.

甜味拾荒者 2024-11-16 09:48:32

使用额外的引号也应该可以正常工作

CustomOrder:="""Cyberspace,Air"",""Land"",""or Sea,Aerospace"""

Using additional quotation marks should also work OK

CustomOrder:="""Cyberspace,Air"",""Land"",""or Sea,Aerospace"""

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文