Sorting with Custom List

Mike2

New Member
Joined
Jan 5, 2019
Messages
41
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:

Variance $Status
(8000)0 Count
(5000)0 Count
(4000)0 Count
(1200)Shortage
1000Overage
1200Overage

<tbody>
</tbody>

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

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,213,510
Messages
6,114,048
Members
448,543
Latest member
MartinLarkin

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