Excel 2010 Limitations of Sorting using custom lists

tonyr63

New Member
Joined
Jan 8, 2014
Messages
21
Hello

I discovered to my surprise there is a limit of 256 characters set on the length of a custom list in Excel 2010. I am trying to combine 3 lists which are outputs from 3 different defect management systems that have the following characteristics.


  1. They have field names in common
  2. The have field names which are equivalent but different
  3. Each list have a varying subset of the required 36 reporting fields
    1. List 1 has 22
    2. List 2 has 19
    3. List 3 has 17
  4. They have collectively 43 different fields
What my procedures do is

  1. Cleans up the largest list re-naming some fields and deleting others and recording the list in the reporting order sorting the columns and adding missing fields as new columns
  2. Similar procedures work on the other lists preparing them to be appended to a single complete list for filtering and charting. Each of these lists have their headings checked against an array of required fields and new columns and field names added as requested and sorted into the order of list 1 ready to be joined up.
I have found if I use VBA I can get the list to sort on my custom field list and get past the 256 character limit, however I have found it does not work consistently and reliably at run time.
The remarkable thing, following extensive testing, it does not fail in a consistent way either.
What I have noticed is once you have run the Macro you for that point forward run the sort manually and your list of far more than 256 characters is there for the choosing and it always sorts correctly when run manually which is different from run time behaviour?
Most of the time it sorts most of the fields correctly but getting the order wrong in different ways each time with occasional complete and correct sorts.
I’ve played around with various settings like .Header = xlYes and No with no success.
Does anyone know what I can do to troubleshoot and resolve?
Has anyone else seen this behaviour that looks a lot like a significant bug as accurately sorting is a core feature of Excel and not something you would expect to be hit and miss.
My questions are:

  1. What is the syntax required to pass variable dimensioned as a Range to both Key:=Range and the .SetRange parameter as I could not get this to work and used (Cells(4, 1), Cells(4, cols)) instead?
  2. Are these any other things I could try to get this to work at run time such as defined a listObject or something?
  3. I notices that sometimes the custom list would seem to appear twice in the custom lists section in advanced options so is there a way to stop it been injected into this list more than once. Is there code to check if the custom list already exists?
  4. Any changes in Excel 2013 concerning the 256 character limit?
Here is the code I was using.
Code:
  Sub ReOrder()
   
  Dim DataR As Range, r As Range, HeadRange As Range, cols As Long, Rs As Long, i As Long
  ThisWorkbook.ActiveSheet.Select
   
  Set r = ActiveSheet.UsedRange
    Rs = r.Rows.Count - 1
    cols = r.Columns.Count
  Set DataR = Range(Cells(4, 1), Cells(Rs, cols))
  Set HeadRange = Range(Cells(4, 1), Cells(4, cols))
   
  ActiveSheet.Sort.SortFields.Clear
  Application.AddCustomList listarray:=Array("Unique ID", "Short Description", _
  "Ship Number", "Reporting Source", "Work Order Number", "Date Identified", _
  "Assignee", "Need Date", "Expected Rectification Date", "Sequencing Priority", _
  "Impact Priority", "System Name", "Configuration Item", "Equipment Name", "Equipment Part/Stock Number", _
  "Equipment Serial Number", "Assembly Name", "Part Number", "Part Serial Number", "MRP Catalogue Number", _
  "Activity", "Location", "Reference", "Department", "Validator", "Detailed Defect Description", "Status", _
  "Updated", "Labels", "Assigned To External Process", "Notify of Creation", "Test Number", "Delivery Certificate Comment", _
  "Responsible Party", "Responsibility Category", "Effect on Capability")
   
  ActiveSheet.Sort.SortFields.Add Key:=Range(Cells(4, 1), Cells(4, cols)) _
          , SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
         "Unique ID, Short Description, Ship Number, Reporting Source, Work Order Number, Date Identified, Assignee, Need Date, Expected Rectification Date, Sequencing Priority, Impact Priority, System Name, Configuration Item, Equipment Name, Equipment Part / Stock Number, Equipment Serial Number, Assembly Name, Part Number, Part Serial Number, MRP Catelogue Number, Activity, Location, Reference, Department, Validator, Detailed Defect Description, Status, Updated, Labels, Assigned To External Process, Notify of Creation, Test Number, Effect on Capability, Responsible Party, Responsibility Category, Department", DataOption:=xlSortNormal
      With ActiveSheet.Sort
          .SetRange Range(Cells(4, 1), Cells(Rs, cols))
          .Header = xlGuess
          .MatchCase = False
          .Orientation = xlLeftToRight
          .SortMethod = xlPinYin
          .Apply
      End With
      
  End Sub
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I confess to not reading the entire novel, but you can create a separate list for sort order, add a column to the table with a lookup to retrieve it, and then sort by that column.
 
Upvote 0
[Table="width:, class:grid"][tr][td] [/td][td]
A​
[/td][td]
B​
[/td][td]
C​
[/td][td]
D​
[/td][td]
E​
[/td][td]
F​
[/td][/tr]
[tr][td]
1​
[/td][td]
Name​
[/td][td]
Sort​
[/td][td]
[/td][td]
Name​
[/td][td]
Sort Pos​
[/td][td]
[/td][/tr]

[tr][td]
2​
[/td][td]Eric[/td][td]
1​
[/td][td][/td][td]Alan[/td][td]
7​
[/td][td]E2: =VLOOKUP(D2, $A$2:$B$12, 2, FALSE)[/td][/tr]

[tr][td]
3​
[/td][td]Hana[/td][td]
2​
[/td][td][/td][td]Barb[/td][td]
5​
[/td][td][/td][/tr]

[tr][td]
4​
[/td][td]Jane[/td][td]
3​
[/td][td][/td][td]Cain[/td][td]
8​
[/td][td][/td][/tr]

[tr][td]
5​
[/td][td]Fran[/td][td]
4​
[/td][td][/td][td]Dana[/td][td]
10​
[/td][td][/td][/tr]

[tr][td]
6​
[/td][td]Barb[/td][td]
5​
[/td][td][/td][td]Eric[/td][td]
1​
[/td][td][/td][/tr]

[tr][td]
7​
[/td][td]Gary[/td][td]
6​
[/td][td][/td][td]Fran[/td][td]
4​
[/td][td][/td][/tr]

[tr][td]
8​
[/td][td]Alan[/td][td]
7​
[/td][td][/td][td]Gary[/td][td]
6​
[/td][td][/td][/tr]

[tr][td]
9​
[/td][td]Cain[/td][td]
8​
[/td][td][/td][td]Hana[/td][td]
2​
[/td][td][/td][/tr]

[tr][td]
10​
[/td][td]Kent[/td][td]
9​
[/td][td][/td][td]Ivan[/td][td]
11​
[/td][td][/td][/tr]

[tr][td]
11​
[/td][td]Dana[/td][td]
10​
[/td][td][/td][td]Jane[/td][td]
3​
[/td][td][/td][/tr]

[tr][td]
12​
[/td][td]Ivan[/td][td]
11​
[/td][td][/td][td]Kent[/td][td]
9​
[/td][td][/td][/tr]
[/table]


The foumula in col E looks up up the sort order in cols A & B. Sort by col E.
 
Upvote 0
Update: I found an error in my code in the long array that spans across the page making it difficult to cross check. For some reason the normal space underscore new line combination does not work with the array list. To get round this I run the sort using he Macro recorder which sorted without error however the Macro recorder produced code with syntax errors too so I was back to square one. The reason I run the recorder was to see how to break the long array and it appears the macro recorder could not do this either.


I still do not understand why I cannot pass variables defined as Ranges to the range section of the Sort function. You would expect to be able to replace:
ActiveSheet.Sort.SortFields.Add Key:=Range(Cells(4, 1), Cells(4, cols)) – with
ActiveSheet.Sort.SortFields.Add Key:=Range(HeadRange) because the following statement is in place earlier in the Module
Set HeadRange = Range(Cells(4, 1), Cells(4, cols)).

I have a similar problem with the section .SetRange Range(Cells(4, 1), Cells(Rs, cols)) which should be replaceable with: - .SetRange Range(DataR) because this is also set as a range. I don’t get a syntax error but a runtime error when I try to use variables no matter what combination of quotes or “=HeadRange” and other I have tried.

I would prefer to not have to repeat the sorting function especially with such a long array of custom sort fields in several places but should be able to define it once and pass variables to single sorting function.
Can anyone show me how to send range variables to a sort function?

 
Upvote 0
Since HeadRange is a Range object, you don't use Range(HeadRange) but just HeadRange:
Code:
ActiveSheet.Sort.SortFields.Add Key:=HeadRange
 
Upvote 0
Hello RoryA

I originally tried that syntax but got an error which may have been because of an issue somewhere else making me try other options.

Thanks for your assistance
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top