Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5945267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:39:26+00:00 2026-05-22T16:39:26+00:00

In VBA, Excel allows sorting values using the CustomOrder parameter to choose the sequence

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:39:27+00:00Added an answer on May 22, 2026 at 4:39 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Excel VBA to a write a UDF. I would like to overload
I am using Excel VBA 2003. Is it possible to create new recordset by
I am getting a very weird error when using VBA in excel. I am
i am adding data from vba excel using adodb into a mysql database everything
In Excel VBA, I am using Excel as datasource for some queries. I am
I'm using VBA excel 2003,SQL 2005 to make a sql query call and inside
I am using VBA for Excel 2010 and randomly receiving the following error: Run-time
I'm using VBA in Excel and am trying to use the popular ShellAndWait subroutine.
Using Excel 2002 and have a VBA app that has some UserForms. The main
I'm using ADODB connection in VBA excel file with Microsoft.Jet.OLEDB.4.0 provider. The file with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.