double custom sort on a range problem

vince g

New Member
Joined
Jul 16, 2020
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I'm still new to VBA but hopefully progressing. That said I'm now stuck on the next stage of a larger project I'm working on.

In short, I need to execute 2 custom sorts on 2 columns of a worksheet range. I've set

I'm using the code below. I've added the 2 custom sorts and can see these within excel as custom lists 5 & 6 ( seen when I don't delete them in VBA - first 4 I know as internal excel ones). The code below does not sort as I specify at all and seems to be alphabetic only. Variable Listn1 has value 5 & listn2 is 6. If I force Listn1 to be 6 the first sort works correctly. Not sure why its not 5. I cannot get the 2nd sort to work at all. Am I missing something obvious here or just plain of the path. I would like to understand my error as part of my education. Alternatively if someone can point be to a better way or some code that would be great. Any help welcome.



Dim Datascope As Range
Dim Listn1 As Long
Dim Listn2 As Long

Set Datascope = Range("A1").CurrentRegion5

Application.AddCustomList Array("Equity", "Bonds", "Property", "Alternate", "Commodity", "Money Market", "Cash")
Listn1 = Application.CustomListCount
Debug.Print "list1"; Listn1
Application.AddCustomList Array("UK", "Global", "Dev", "Emerg", "Europe", "US", _
"Japan", "AsiaPac ex Jap", "Latin America", "Themed", "Frontier", "Country")
Listn2 = Application.CustomListCount
Debug.Print "list2"; Listn2


With Datascope
.Sort Key1:=Range("I1", Range("I1").End(xlDown)), OrderCustom:=Listn1, Header:=xlYes, _
Key2:=Range("J1", Range("J1").End(xlDown)), OrderCustom:=Listn2, Header:=xlYes

Application.DeleteCustomList Listn1
Application.DeleteCustomList Listn2

End With

Immediate Windows shows
list1 5
list2 6

The sheet looks like this post run of the code - copied a section here where 1st coln is actually col I and 2nd is Col J in my main sheet.



CoreAssetClassRegionFactor 1Factor 2
EquityDevLargeValue
EquityDevLargeBlend
EquityDevLargeGrowth
EquityDevSmallBlend
EquityEmergLargeBlend
EquityEmergLargeBlend
EquityEmergSmall-MediumBlend
EquityEmergSmallValue
EquityEmergBlendValue
EquityEmergsmallBlend
EquityEmergSmallBlend
EquityEmergBlendValue
EquityEuropeMediumGrowth
EquityEuropeLargeGrowth
EquityUKSmallgrowth
EquityUKSmallgrowth
EquityUKLargeValue
EquityUKLargeBlend
EquityUKSmallgrowth
EquityUKMediumBlend
EquityUKLargeValue
EquityUKSmallValue
EquityUKSmallBlend
BondsDevStrategicGov+Corp
BondsDevStrategicGov+Corp
BondsDevStrategicGov+Corp
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You cannot use custom sort on two columns with the old style code, you will need to use the new way of sorting.
 
Upvote 0
Fluff hi, I'm new to this so was not aware there was an old and new approach. Could you point me at what is considered the new approach so I can try and understand more and solve my issue. Thx
 
Upvote 0
Easiest way is to run your code, without these two lines
VBA Code:
Application.DeleteCustomList Listn1
Application.DeleteCustomList Listn2
Then turn on the macro recorder & record yourself manually sorting the data. Turn f the recorder & you have the basis for your code.
 
Upvote 0
The macro recorder code can then be cleaned up, so you would get
VBA Code:
Sub Vinceg()
   Dim Lst1 As String, Lst2 As String
   Dim ws As Worksheet
   
   Set ws = ActiveSheet
   Lst1 = "Equity, Bonds, Property, Alternate, Commodity, Money Market, Cash"
   Lst2 = "UK, Global, Dev, Emerg, Europe, US, Japan, AsiaPac ex ***, Latin America, Themed,Frontier, Country"
   With ws.Sort
      .SortFields.Clear
      .SortFields.Add Range("I1"), xlSortOnValues, , Lst1, xlSortNormal
      .SortFields.Add Range("J1"), xlSortOnValues, , Lst2, xlSortNormal
      .SetRange Range("A1").CurrentRegion
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
End Sub
 
Upvote 0
Fluff hi,

many thanks for the code snippet and taking the time to generate it and help me. It works great and I understand it. On a last point I would be interested to understand more on the old and new point you made prior. I assume the 'With' construct is of the new form. Could you point me to somewhere here or elsewhere I could read a little more on this so I understand the history better. If not fine, and thx again for your help.
 
Upvote 0
The With statement has been around a long time & is just a form of "shorthand" so that you don't have to put ws.Sort at the start of each line.
Unfortunately I don't know of any sites that explain the difference between between the two types of sorting, although the "new" way came into being with the release of Excel 2007.
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

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