+ Reply to Thread
Results 1 to 16 of 16

Custom sort without custom list

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Custom sort without custom list

    Hello everybody
    I have a sheet named "Data" which contains cutom list on which I need to sort column C in sheets("Report")
    I don't want to use custom list feature ( Application.AddCustomList )


    I need to depend on the custom list in range("B2:B8") in sheets("Data")

    I found UDF function but can't apply it
    This my try but I got an error

    Sub CustomSort()
        Dim Sh As Worksheet, LR As Long
        Set Sh = Sheets("Report")
        LR = Sh.Cells(Rows.Count, 1).End(xlUp).Row
        Sh.Range("A4:E" & LR).Sort Key1:=Range("C4"), OrderCustom:=SortItems, Header:=xlYes
    End Sub
    
    Function SortItems() As String
        Dim ArrSort() As Variant
        Dim RngSort As Range
        Dim I As Long
        
        With Worksheets("Data")
            Set RngSort = .Range("B1:B" & .Cells(Rows.Count, "B").End(xlUp).Row)
        End With
        
        ReDim ArrSort(1 To RngSort.Rows.Count)
        For I = 2 To UBound(ArrSort)
            ArrSort(I) = RngSort(I, 1)
        Next
    
        SortItems = Join(ArrSort, ",")
    End Function
    The error is at this line
    Sh.Range("A4:E" & LR).Sort Key1:=Range("C4"), OrderCustom:=SortItems, Header:=xlYes
    Sort method of range failed 1004
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Custom sort without custom list

    You must use Sort object not Sort method:
    Sub CustomSort()
        Dim Sh As Worksheet, LR As Long
        Set Sh = Sheets("Report")
        LR = Sh.Cells(Rows.Count, 1).End(xlUp).Row
        With Sh.Sort
            .SortFields.Clear
            .SortFields.Add Sh.Range("C4"), CustomOrder:=SortItems
            .SetRange Sh.Range("A4:E" & LR)
            .Header = xlYes
            .Apply
        End With
    End Sub
    Sortitems function returns string with leading comma but this does not seem to matter.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Custom sort without custom list

    Mr. Izandol
    Thank you very very much for this correction
    It works great

    Another point in this thread I need to ask about .. what if I have a nother cutom list in another range and I need to apply it too
    How can I do that?
    See the attachment for second custom list
    Attached Files Attached Files

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Custom sort without custom list

    You may add another sort field in same way

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Custom sort without custom list

    Last point I need add column E but sort it in normal way in ascending order
    It is now OK with column C and D but not ok in column E
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Custom sort without custom list

    Thanks for reply

    I tried that and everything is ok
    Sub CustomSort()
        Dim Sh As Worksheet, LR As Long
        Set Sh = Sheets("Report")
        LR = Sh.Cells(Rows.Count, 1).End(xlUp).Row
        With Sh.Sort
            .SortFields.Clear
            .SortFields.Add Sh.Range("C4"), CustomOrder:=SortItemsA
            .SetRange Sh.Range("A4:E" & LR)
            .Header = xlYes
            .Apply
        End With
        
        With Sh.Sort
            .SortFields.Clear
            .SortFields.Add Sh.Range("D4"), CustomOrder:=SortItemsB
            .SetRange Sh.Range("A4:E" & LR)
            .Header = xlYes
            .Apply
        End With
    End Sub
    
    Function SortItemsA() As String
        Dim ArrSort() As Variant
        Dim RngSort As Range
        Dim I As Long
        
        With Worksheets("Data")
            Set RngSort = .Range("B1:B" & .Cells(Rows.Count, "B").End(xlUp).Row)
        End With
        
        ReDim ArrSort(1 To RngSort.Rows.Count)
        For I = 2 To UBound(ArrSort)
            ArrSort(I) = RngSort(I, 1)
        Next
    
        SortItemsA = Join(ArrSort, ",")
    End Function
    
    Function SortItemsB() As String
        Dim ArrSort() As Variant
        Dim RngSort As Range
        Dim I As Long
        
        With Worksheets("Data")
            Set RngSort = .Range("C1:C" & .Cells(Rows.Count, "C").End(xlUp).Row)
        End With
        
        ReDim ArrSort(1 To RngSort.Rows.Count)
        For I = 2 To UBound(ArrSort)
            ArrSort(I) = RngSort(I, 1)
        Next
    
        SortItemsB = Join(ArrSort, ",")
    End Function
    But is there an efficient way to make the function more flexible as I intend to add more than two custom lists ..?
    Last edited by YasserKhalil; 01-12-2016 at 08:36 AM.

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Custom sort without custom list

    That is not problem - you must only create new sort list and use with CustomOrder on new sortfield.

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Custom sort without custom list

    Quote Originally Posted by Izandol View Post
    That is not problem - you must only create new sort list and use with CustomOrder on new sortfield.
    I know but I am searching for a way to merge all in one function
    instead of creating one by one
    Function SortItemsA() As String
    Function SortItemsB() As String
    Function SortItemsC() As String
    to be only in one
    Function SortItems() As String
    so as to have just one function for all custom lists

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Custom sort without custom list

    maybe so
    Sub ertert()
    Dim rng As Range: Set rng = Sheets("Report").Range("A4").CurrentRegion
    With Sheets("Report").Sort
        .SortFields.Clear
        .SortFields.Add Key:=rng.Columns(5), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
        .SortFields.Add Key:=rng.Columns(3), CustomOrder:=SortItems(2)
        .SortFields.Add Key:=rng.Columns(4), CustomOrder:=SortItems(3)
        .SetRange rng
        .Header = xlYes
        .Apply
    End With
    End Sub
    
    Function SortItems(clm As Long) As String
    With Worksheets("Data")
        SortItems = Join(Application.Transpose(.Range(.Cells(2, clm), .Cells(Rows.Count, clm).End(xlUp))), ",")
    End With
    End Function

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Custom sort without custom list

    Thanks a lot I solved last point by putting this line
    .SortFields.Add Key:=Rng.Columns(5), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    After these two lines
            .SortFields.Add Key:=Rng.Columns(3), CustomOrder:=SortItems(2)
            .SortFields.Add Key:=Rng.Columns(4), CustomOrder:=SortItems(3)
    Last edited by YasserKhalil; 01-12-2016 at 10:13 AM.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Custom sort without custom list

    Mr. Nilem
    I really apprciate this great and wonderful help
    It is very very well after I removed this line and the results are ok for column C & D
    .SortFields.Add Key:=Rng.Columns(5), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    After adding this line again
    I got incorrect results ..
    My problem now is with column E only ..but as for custom sort it is ok

  12. #12
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Custom sort without custom list

    I would suggest more general function:
    Sub CustomSort()
        Dim Sh As Worksheet, LR As Long
        Dim ws as Worksheet
        Set Sh = Sheets("Report")
        Set ws = worksheets("Data")
        LR = Sh.Cells(Rows.Count, 1).End(xlUp).Row
        With Sh.Sort
            .SortFields.Clear
            .SortFields.Add Sh.Range("C4"), CustomOrder:=SortItems(ws.Range("B2", ws.Cells(Rows.Count, "B").End(xlUp)))
            .SortFields.Add Sh.Range("D4"), CustomOrder:=SortItems(ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp)))
            .SortFields.Add Key:=Sh.Range("E4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
            .SetRange Sh.Range("A4:E" & LR)
            .Header = xlYes
            .Apply
        End With
    End Sub
    
    Function SortItems(rngSort As Range) As String
        Dim ArrSort() As Variant
        Dim I As Long
        
        ReDim ArrSort(1 To rngSort.Rows.Count)
        For I = 1 To UBound(ArrSort)
            ArrSort(I) = rngSort(I, 1)
        Next
    
        SortItems = Join(ArrSort, ",")
    End Function
    Last edited by Izandol; 01-12-2016 at 01:00 PM. Reason: Miss object reference

  13. #13
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Custom sort without custom list

    Posted two times

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Custom sort without custom list

    Thank you very much Mr. Izandol
    I got an error at this line
    .SortFields.Add Sh.Range("C4"), CustomOrder:=SortItems(.Range("B2", .Cells(Rows.Count, "B").End(xlUp)))
    'Error 438 : Object doesn't support this property or method

  15. #15
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Custom sort without custom list

    Yes - I fix this just now. Please use changed code.

  16. #16
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Custom sort without custom list

    Great & Excellent & Awesome. Thank you very much Mr. Izandol
    I'm very pleased with your sharing in this thread
    Mr. Nilem
    Thanks a lot for great and wonderful help you offered
    Kind Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Custom Sort List
    By druid98 in forum Excel General
    Replies: 0
    Last Post: 10-10-2011, 07:51 PM
  2. custom list does not sort
    By Matt Lunn in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-06-2005, 06:05 AM
  3. [SOLVED] custom list does not sort
    By Matt Lunn in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 09-06-2005, 04:05 AM
  4. [SOLVED] Excel sort by Fill Color by custom list sort
    By Dash4Cash in forum Excel General
    Replies: 2
    Last Post: 07-29-2005, 06:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1