Sorting with Custom List

Mike2

New Member
Joined
Jan 5, 2019
Messages
43
Hello,

I am looking to have two sorting done with my code. First order is from Custom List and the 2nd order is Smallest to Largest.

Here is my code:

Code:
Dim sStatus As Range
Dim sVarValue As Range

Application.AddCustomList ListArray:=Array("0 Count","Shortage","Overage")

Set sStatus = Rows(1).Find("Status")
Set sVarValue=Rows(1).Find("Variance $")

ActiveSheet.Sort.SortFields.Clear

Range("A1").CurrentRegion.Resize(Range("A1").CurrentRegion.Rows.Count - 1).Sort Key1:=sStatus, Order1:=xlAscending, Ordercustom:=Application.CustomListCount, _
        Key2:=sVarValue, Order2:=xlAscending, Header:=xlYes, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

The result of the Code does not sort the way i wanted to be.

I want the result of the sorting to be as following:

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Variance $[/TD]
[TD]Status[/TD]
[/TR]
[TR]
[TD](8000)[/TD]
[TD]0 Count[/TD]
[/TR]
[TR]
[TD](5000)[/TD]
[TD]0 Count[/TD]
[/TR]
[TR]
[TD](4000)[/TD]
[TD]0 Count[/TD]
[/TR]
[TR]
[TD](1200)[/TD]
[TD]Shortage[/TD]
[/TR]
[TR]
[TD]1000[/TD]
[TD]Overage[/TD]
[/TR]
[TR]
[TD]1200[/TD]
[TD]Overage[/TD]
[/TR]
</tbody>[/TABLE]

Any help from your expertise is much appreciated...thank you
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try
Code:
Dim sStatus As Range
Dim sVarValue As Range

Application.AddCustomList ListArray:=Array("0 Count", "Shortage", "Overage")

Set sStatus = Rows(1).Find("Status")
Set sVarValue = Rows(1).Find("Variance $")

ActiveSheet.Sort.SortFields.Clear

Range("A1").CurrentRegion.Sort Key1:=sStatus, Order1:=xlAscending, Ordercustom:=Application.CustomListCount + 1, _
        Key2:=sVarValue, Order2:=xlAscending, Header:=xlYes, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
 
Upvote 0
Thank you higrm and Fluff for your quick respond.
@Fluff, your suggestion with +1 at the end of CustomListCount works.
But how does it calculate ? I tried looking up at the Microsoft reference site, but it does not give much detail explanation...thank you
 
Upvote 0
The count for CustomList starts at zero, so to get the last sort in the list you need to add 1 to the count.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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