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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,544
Office Version
  1. 365
Platform
  1. Windows
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
 

Mike2

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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,544
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

The count for CustomList starts at zero, so to get the last sort in the list you need to add 1 to the count.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,544
Office Version
  1. 365
Platform
  1. Windows
You're welcome
 

Watch MrExcel Video

Forum statistics

Threads
1,129,512
Messages
5,636,764
Members
416,939
Latest member
Rajakumaran

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
Top