Application Custom List

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
Hi, can anyone help with Application Custom List to Sort Data

I have a command button on sht2 with the following code to sort data on sht4 but excel will not save and crashes after running the macro. I have tried tinkering around a little with the code but keep getting errors.

For example I have tried changing the +2 to +6 in the code as there is 5 Default Custom Lists already exiting within excel and this allowed me to save after running the code but did not sort on sht4 the way it should?

Any help would be appreciated

Code:
Private Sub CommandButton110_Click()
    Dim Sht2 As Worksheet: Set Sht2 = Sheet02
    Dim Sht4 As Worksheet: Set Sht4 = Sheet04
    Dim ListNum As Long
    Dim lRowA As Long
    Dim lRowM As Long
    
        Application.ScreenUpdating = False
            
        lRowA = Sht4.Cells(Rows.Count, "A").End(xlUp).Row
        lRowM = Sht4.Cells(Rows.Count, "M").End(xlUp).Row
        
        Sht4.Range("A2:A" & lRowA) = _
            Sht4.Range("E2:E" & lRowA).Value
        Sht4.Range("M2:M" & lRowM) = _
            Sht4.Range("Q2:Q" & lRowM).Value
                
        ListNum = Application.CustomListCount + 2
        Application.AddCustomList Array("0", "1", "-1", "2", "-2", "3", "-3", "4", "-4", "5", "-5", "6", "-6", "7", "-7", "8", "-8", "9", "-9", "10", "-10", "11", "-11", "12", "-12", "13", "-13", "14", "-14", "15", "-15", "16", "-16", "17", "-17", "18", "-18", "19", "-19", "20", "-20", "21", "-21", "22", "-22", "23", "-23", "24", "-24", "25", "-25", "26", "-26", "27", "-27", "28", "-28", "29", "-29", "30", "-30", "31", "-31", "32", "-32", "33", "-33", "34", "-34", "35", "-35", "36", "-36", "37", "-37", "38", "-38", "39", "-39", "40", "-40", "41", "-41", "42", "-42", "43", "-43", "44", "-44", "45", "-45", "46", "-46", "47", "-47", "48", "-48", "49", "-49", "50", "-50", "51", "-51", "52", "-52", "53", "-53", "54", "-54", "55", "-55", "56", "-56", "57", "-57", "58", "-58", "59", "-59", "60")
            With Sht4
                .Range("A2:K" & lRowA).Sort Key1:=.Range("E2"), order1:=xlAscending, _
                    Header:=xlNo, OrderCustom:=ListNum
                .Range("M2:W" & lRowM).Sort Key1:=.Range("Q2"), order1:=xlAscending, _
                    Header:=xlNo, OrderCustom:=ListNum
            End With
        ListNum = ListNum - 1
        Application.DeleteCustomList ListNum
        
    Application.ScreenUpdating = True
    
End Sub

Regards


pwill
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try adding this line
Code:
                 [COLOR=#ff0000] .Range("A1").Sort key1:=.Range("A1"), ordercustom:=1[/COLOR]
            End With
 
Upvote 0
Thanks Fluff,

That almost worked, it did save this time but the sort came out wrong with "A:K" and sorted correct with "M:W"

ie the sort is in reverse with "A:K" and the headers have gone to the bottom

ABCDEFGHIJKMNOPQRSTUVWXYZ
1-2432583-2-3-3-46-68REFDateCnt1Cnt2ABCDEFG
20432571011-1-5-5-60432571
01-1-2-1618-21
3043258201-1-3-45-50432582
0811-11131735
404325940034-59100432594
0-12-18-20-30-31
5043259501-1-2-2450432594
0-2-12-13171820
604326060-23-4-56-60432595
0-513-21-22-27-28
7043260701-233-4-100432606
0-2-67-12-25-27
8043261800012560432607
0-1-2-38918
904326190-12-34480432618
025-671328
100432621000-1-23560432618
0-6-8-11-1315-19
1104326211001-124-60432618
0-15-21-23-26-28-34
120432631201-24-6-780432619
0-1720293035
13REFDateCnt1Cnt2ABCDEFG04326210
04-1617-21-26-30
1404326210
0-13-17-33-38-43-47
1504326211
0136-11-30-31
1604326211
0-125-12-31-32
1704326312
012-35-23-32
181432571
1234-13-1821
191432571
162021222340
201432582
19-1012141836
211432595
12-4-5182336
221432607
12-7-8-9-1011
231432618
17912142035
2414326211
1247-10-29-30
2514326312
1103033343538
26-1432571
-1-2-3-416-18-23
27
28

<tbody>
</tbody>
 
Upvote 0
And I have the code as

Code:
Private Sub CommandButton110_Click()
    Dim Sht2 As Worksheet: Set Sht2 = Sheet02
    Dim Sht4 As Worksheet: Set Sht4 = Sheet04
    Dim ListNum As Long
    Dim lRowA As Long
    Dim lRowM As Long
    
        Application.ScreenUpdating = False
            
        lRowA = Sht4.Cells(Rows.Count, "A").End(xlUp).Row
        lRowM = Sht4.Cells(Rows.Count, "M").End(xlUp).Row
        
        Sht4.Range("A2:A" & lRowA) = _
            Sht4.Range("E2:E" & lRowA).Value
        Sht4.Range("M2:M" & lRowM) = _
            Sht4.Range("Q2:Q" & lRowM).Value
                
        ListNum = Application.CustomListCount + 2
        Application.AddCustomList Array("0", "1", "-1", "2", "-2", "3", "-3", "4", "-4", "5", "-5", "6", "-6", "7", "-7", "8", "-8", "9", "-9", "10", "-10", "11", "-11", "12", "-12", "13", "-13", "14", "-14", "15", "-15", "16", "-16", "17", "-17", "18", "-18", "19", "-19", "20", "-20", "21", "-21", "22", "-22", "23", "-23", "24", "-24", "25", "-25", "26", "-26", "27", "-27", "28", "-28", "29", "-29", "30", "-30", "31", "-31", "32", "-32", "33", "-33", "34", "-34", "35", "-35", "36", "-36", "37", "-37", "38", "-38", "39", "-39", "40", "-40", "41", "-41", "42", "-42", "43", "-43", "44", "-44", "45", "-45", "46", "-46", "47", "-47", "48", "-48", "49", "-49", "50", "-50", "51", "-51", "52", "-52", "53", "-53", "54", "-54", "55", "-55", "56", "-56", "57", "-57", "58", "-58", "59", "-59", "60")
            With Sht4
                .Range("A2:K" & lRowA).Sort Key1:=.Range("E2"), order1:=xlAscending, _
                    Header:=xlNo, OrderCustom:=ListNum
                .Range("M2:W" & lRowM).Sort Key1:=.Range("Q2"), order1:=xlAscending, _
                    Header:=xlNo, OrderCustom:=ListNum
                [COLOR=#ff0000].Range("A1").Sort key1:=.Range("A1"), ordercustom:=1[/COLOR]
            End With
        ListNum = ListNum - 1
        Application.DeleteCustomList ListNum
        
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Try adding this line
Code:
                 [COLOR=#ff0000] .Range("A1").Sort key1:=.Range("A1"), ordercustom:=1[/COLOR]
            End With


Thanks Fluff,

I have managed to work it out, everything sorts as it should now and also allows me to save after running the code :)
This line did the trick

Code:
[COLOR=#ff0000][FONT=Verdana][LEFT]                             .Range("A1", "M1").Sort key1:=.Range("A1", "M1"), ordercustom:=1[LEFT][COLOR=#574123][FONT=monospace][I]            End With[/I][/FONT][/COLOR][/LEFT]
[/LEFT]
[/FONT][/COLOR]



thanks again

pwill
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,812
Members
449,048
Latest member
greyangel23

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