+ Reply to Thread
Results 1 to 6 of 6

Creating a custom sorting

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-16-2009
    Location
    NC
    MS-Off Ver
    Office 2016
    Posts
    164

    Creating a custom sorting

    Hello everyone,

    I am trying to create a custom sorting in VB. My sorting needs to be first on column B, then C and last on G. My data does not have headers.
    How do I create such a custom sorting?

    Thanks in advance?
    Last edited by sentinela; 01-11-2010 at 05:59 PM. Reason: Solved

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Creating a custom sorting

    Record a macro going through each of the subsequent sorting steps. Post the code and workbook for help on cleaning up the code.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Forum Contributor
    Join Date
    03-16-2009
    Location
    NC
    MS-Off Ver
    Office 2016
    Posts
    164

    Re: Creating a custom sorting

    Palmetto,
    Thanks for the answer, here is the code recorded.

    Sub Macro2()
    
        ActiveWorkbook.Worksheets("Readings").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Readings").Sort.SortFields.Add Key:=Range( _
            "B1:B3249"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.Worksheets("Readings").Sort.SortFields.Add Key:=Range( _
            "C1:C3249"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortTextAsNumbers
        ActiveWorkbook.Worksheets("Readings").Sort.SortFields.Add Key:=Range( _
            "G1:G3249"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortTextAsNumbers
        With ActiveWorkbook.Worksheets("Readings").Sort
            .SetRange Range("A1:I3249")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveWindow.SmallScroll Down:=-12
        Selection.EntireRow.Insert
        Range("A1").Select
    End Sub
    Thanks for the help!

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Creating a custom sorting

    Maybe like this:
    Sub Macro2()
        With ActiveWorkbook.Worksheets("Readings").Sort
            .SortFields.Clear
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SetRange Range("A1:I3249")
            
            .SortFields.Add Key:=Range("B1:B3249"), _
                            SortOn:=xlSortOnValues, _
                            Order:=xlAscending, _
                            DataOption:=xlSortNormal
            
            .SortFields.Add Key:=Range("C1:C3249"), _
                            SortOn:=xlSortOnValues, _
                            Order:=xlAscending, _
                            DataOption:=xlSortTextAsNumbers
            
            .SortFields.Add Key:=Range("G1:G3249"), _
                            SortOn:=xlSortOnValues, _
                            Order:=xlAscending, _
                            DataOption:=xlSortTextAsNumbers
            .Apply
        End With
    End Sub
    If you create a dynamic named range for your data, you could avoid the hard-coded references:
    Sub Macro2()
        Dim rSort As Range
        
        Set rSort = Range("myData")
        
        With ActiveWorkbook.Worksheets("Readings").Sort
            .SortFields.Clear
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SetRange rSort
            
            .SortFields.Add Key:=rSort.Columns(2), _
                            SortOn:=xlSortOnValues, _
                            Order:=xlAscending, _
                            DataOption:=xlSortNormal
            
            .SortFields.Add Key:=rSort.Columns(3), _
                            SortOn:=xlSortOnValues, _
                            Order:=xlAscending, _
                            DataOption:=xlSortTextAsNumbers
            
            .SortFields.Add Key:=rSort.Columns(7), _
                            SortOn:=xlSortOnValues, _
                            Order:=xlAscending, _
                            DataOption:=xlSortTextAsNumbers
            .Apply
        End With
    End Sub
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Creating a custom sorting

    In conjunction with shg's suggestion to use a dynamic named range, see this link for help on the topic.

  6. #6
    Forum Contributor
    Join Date
    03-16-2009
    Location
    NC
    MS-Off Ver
    Office 2016
    Posts
    164

    Re: Creating a custom sorting

    Thank you very much shg and palmetto.

    I appreciate all your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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