+ Reply to Thread
Results 1 to 6 of 6

Problem in a custom sort code

Hybrid View

  1. #1
    Registered User
    Join Date
    05-27-2021
    Location
    Strasbourg, France
    MS-Off Ver
    2010
    Posts
    28

    Question Problem in a custom sort code

    Hello the forum,

    I have a problem in the code of my workbook, code that I attach in the topic with the file. I would like to do a custom sorting in all the tabs in column E by means of a vba code. Because this file will be shared and it is not possible to export a personal sort...

    Thank you in advance for your help.


    Private Sub CommandButton1_Click()
        Dim Ws As Worksheet
        On Error Resume Next
        For Each Ws In Worksheets
            If Ws.Name <> "CONFIG" Then
                With Ws.ListObjects(1)
        Sort.SortFields.Clear
        Sort.SortFields.Add _
            Key:=Range("E:E"), SortOn:=xlSortOnValues, Order:= _
            xlAscending, CustomOrder:=" 1-0-0,1-1-0,1-1-1,1-1-2,1-1-3,1-1-4,1-2-0,1-2-1,1-2-2,1-2-3,1-2-4,1-3-0,1-3-1,1-3-2,1-3-3,1-3-4,1-4-0,1-4-1,1-4-2,1-4-3,1-4-4,1-5-0,1-5-1,1-5-2,1-5-3,1-5-4,1-6-0,1-6-1,1-6-2,1-6-3,1-6-4,1-7-0,1-7-1,1-7-2,1-7-3,1-7-4,1-8-0,2-0-0,3-0-0,2-1-0,2-1-1,2-1-2,2-1-3,3-1-1,3-1-2,3-1-3,3-1-4,2-2-0,2-2-1,2-2-2,2-2-3,3-2-1,3-2-2,3-2-3,3-2-4,2-3-0,2-3-1,2-3-2,2-3-3,3-3-1,3-3-2,3-3-3,3-3-4,2-4-0,2-4-1,2-4-2,2-4-3,3-4-1,3-4-2,3-4-3,3-4-4,2-5-0,2-5-1,2-5-2,2-5-3,3-5-1,3-5-2,3-5-3,3-5-4,3-6-1,3-6-2,3-6-3,3-6-4,2-6-0,2-6-1,2-6-2,2-6-3,3-7-1,3-7-2,3-7-3,3-7-4,3-8-1,3-8-2,3-8-3,3-8-4,2-7-0,2-7-1,2-7-2,2-7-3,2-8-0,5-1-0,5-2-0,5-3-0,4-1-0,4-2-0,4-3-0,4-4-0,5-4-0,5-5-0,4-5-0,4-6-0,5-6-0,5-6-1,5-7-0,4-7-0,4-8-0,5-8-0,5-8-1,5-9-0,4-9-0,4-10-0,5-10-0,5-11-0,6-1-0,6-2-0,6-3-0,6-4-0,6-5-0,6-6-0,6-7-0,6-8-0,6-9-0,6-10-0,6-11-0,0,Sur chaîne,A determiner,Servantes visserie", DataOption:=xal
        With Ws.ListObjects(1).Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        End With
        End If
        Next
    End Sub
    Attached Files Attached Files
    Last edited by Benten667; 06-25-2021 at 10:20 AM. Reason: File uptade

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

    Re: Problem in a custom sort code

    Hi Benten667,
    try this
    Sub SortColumnsE()
    Dim Ws As Worksheet, Rng As Range
    
    For Each Ws In ThisWorkbook.Sheets
        If Ws.Name <> "CONFIG" Then
            Set Rng = Ws.Range("D3").CurrentRegion
            With Ws.Sort
                .SortFields.Clear
                .SortFields.Add Key:=Rng.Columns(2), SortOn:=xlSortOnValues, Order:= _
                                xlAscending, CustomOrder:="1-0-0,1-1-0,1-1-1,1-1-2,1-1-3,1-1-4,1-2-0,1-2-1," & _
                                                           "1-2-2,1-2-3,1-2-4,1-3-0,1-3-1,1-3-2,1-3-3,1-3-4,1-4-0,1-4-1,1-4-2,1-4-3,1-4-4," & _
                                                           "1-5-0,1-5-1,1-5-2,1-5-3,1-5-4,1-6-0,1-6-1,1-6-2,1-6-3,1-6-4,1-7-0,1-7-1,1-7-2," & _
                                                           "1-7-3,1-7-4,1-8-0,2-0-0,3-0-0,2-1-0,2-1-1,2-1-2,2-1-3,3-1-1,3-1-2,3-1-3,3-1-4," & _
                                                           "2-2-0,2-2-1,2-2-2,2-2-3,3-2-1,3-2-2,3-2-3,3-2-4,2-3-0,2-3-1,2-3-2,2-3-3,3-3-1," & _
                                                           "3-3-2,3-3-3,3-3-4,2-4-0,2-4-1,2-4-2,2-4-3,3-4-1,3-4-2,3-4-3,3-4-4,2-5-0,2-5-1," & _
                                                           "2-5-2,2-5-3,3-5-1,3-5-2,3-5-3,3-5-4,3-6-1,3-6-2,3-6-3,3-6-4,2-6-0,2-6-1,2-6-2," & _
                                                           "2-6-3,3-7-1,3-7-2,3-7-3,3-7-4,3-8-1,3-8-2,3-8-3,3-8-4,2-7-0,2-7-1,2-7-2,2-7-3," & _
                                                           "2-8-0,5-1-0,5-2-0,5-3-0,4-1-0,4-2-0,4-3-0,4-4-0,5-4-0,5-5-0,4-5-0,4-6-0,5-6-0," & _
                                                           "5-6-1,5-7-0,4-7-0,4-8-0,5-8-0,5-8-1,5-9-0,4-9-0,4-10-0,5-10-0,5-11-0,6-1-0,6-2-0," & _
                                                           "6-3-0,6-4-0,6-5-0,6-6-0,6-7-0,6-8-0,6-9-0,6-10-0,6-11-0,0,Sur chaîne,A determiner,Servantes visserie"
                .SetRange Rng
                .Header = xlYes
                .MatchCase = False
                .Apply
            End With
        End If
    Next Ws
    MsgBox "Ok", 64
    End Sub
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,527

    Re: Problem in a custom sort code

    Benten667
    The main problem with your code is that you are referencing a ListObject that is not there. Neither sheet contains a table (ListObject).
    Like nilem, but I add a custom list temporarily.
    Private Sub CommandButton1_Click()
        Dim Ws          As Worksheet
        Dim Rng         As Range
        Dim lLstCnt     As Long
    
    
        Application.AddCustomList listArray:=Split("1-0-0,1-1-0,1-1-1,1-1-2,1-1-3,1-1-4,1-2-0,1-2-1,1-2-2,1-2-3,1-2-4," & _
                                                   "1-3-0,1-3-1,1-3-2,1-3-3,1-3-4,1-4-0,1-4-1,1-4-2,1-4-3,1-4-4," & _
                                                   "1-5-0,1-5-1,1-5-2,1-5-3,1-5-4,1-6-0,1-6-1,1-6-2,1-6-3,1-6-4," & _
                                                   "1-7-0,1-7-1,1-7-2,1-7-3,1-7-4,1-8-0,2-0-0,3-0-0," & _
                                                   "2-1-0,2-1-1,2-1-2,2-1-3,3-1-1,3-1-2,3-1-3,3-1-4,2-2-0,2-2-1,2-2-2,2-2-3," & _
                                                   "3-2-1,3-2-2,3-2-3,3-2-4,2-3-0,2-3-1,2-3-2,2-3-3,3-3-1,3-3-2,3-3-3,3-3-4," & _
                                                   "2-4-0,2-4-1,2-4-2,2-4-3,3-4-1,3-4-2,3-4-3,3-4-4,2-5-0,2-5-1,2-5-2,2-5-3," & _
                                                   "3-5-1,3-5-2,3-5-3,3-5-4,3-6-1,3-6-2,3-6-3,3-6-4,2-6-0,2-6-1,2-6-2,2-6-3," & _
                                                   "3-7-1,3-7-2,3-7-3,3-7-4,3-8-1,3-8-2,3-8-3,3-8-4,2-7-0,2-7-1,2-7-2,2-7-3,2-8-0," & _
                                                   "5-1-0,5-2-0,5-3-0,4-1-0,4-2-0,4-3-0,4-4-0,5-4-0,5-5-0,4-5-0,4-6-0," & _
                                                   "5-6-0,5-6-1,5-7-0,4-7-0,4-8-0,5-8-0,5-8-1,5-9-0,4-9-0,4-10-0,5-10-0,5-11-0," & _
                                                   "6-1-0,6-2-0,6-3-0,6-4-0,6-5-0,6-6-0,6-7-0,6-8-0,6-9-0,6-10-0,6-11-0,0," & _
                                                   "Sur chaîne,A determiner,Servantes visserie", ",")
    
        lLstCnt = Application.CustomListCount
    
        'On Error Resume Next
        For Each Ws In Worksheets
            If UCase(Ws.Name) <> "CONFIG" Then
                With Ws
                    Set Rng = .Range("D3").CurrentRegion
                    With .Sort
                        .SortFields.Clear
                        .SortFields.Add Key:=Rng.Columns(2), _
                                        SortOn:=xlSortOnValues, _
                                        Order:=xlAscending, _
                                        CustomOrder:=lLstCnt, _
                                        DataOption:=xlSortNormal
                        .SetRange Rng
                        .Header = xlYes
                        .MatchCase = False
                        .Orientation = xlTopToBottom
                        .SortMethod = xlPinYin
                        .Apply
                    End With
                End With
            End If
        Next Ws
    
        Application.DeleteCustomList ListNum:=lLstCnt
        ActiveCell.Activate
    
    End Sub
    Artik

  4. #4
    Registered User
    Join Date
    05-27-2021
    Location
    Strasbourg, France
    MS-Off Ver
    2010
    Posts
    28

    Re: Problem in a custom sort code

    Hello, thank you both for your feedback which actually works in the excel case. I have simplified the basic file to make it easier to understand and access the code...

    But unfortunately I forgot to format the tables hence the presence of the "ListObject" function in my original code.

    Sorry I put the updated file in the topic above.

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

    Re: Problem in a custom sort code

    change
    Set Rng = Ws.ListObjects(1).Range
    instead of
    Set Rng = Ws.Range("D3").CurrentRegion

  6. #6
    Registered User
    Join Date
    05-27-2021
    Location
    Strasbourg, France
    MS-Off Ver
    2010
    Posts
    28

    Re: Problem in a custom sort code

    ^^ Lazy when you hold us !
    I should have been able to see this!

    Anyway, thanks a lot Nilem! It's nice to have competent help

+ 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. [SOLVED] vba code - Custom Sort on row1 - Left to Right
    By excel_googler in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-31-2021, 12:50 PM
  2. [SOLVED] Custom Sort VBA Problem
    By benjhardie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-11-2019, 06:23 AM
  3. [SOLVED] custom sort simplify code
    By hopefulhart in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-11-2017, 09:17 PM
  4. Code For A Custom Sort Using A List On Another Sheet
    By rockyw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2016, 08:51 PM
  5. Custom List Sort - A struggling problem
    By rakesh_sss in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 10-29-2012, 02:50 AM
  6. Custom sort order problem
    By mfmcclellan in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-04-2012, 05:05 PM
  7. Add Custom Sort List to VBA code
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2009, 09:44 AM

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