1. Welcome to Chandoo.org Forums. Short message for you

    Hi Guest,

    Thanks for joining Chandoo.org forums. We are here to make you awesome in Excel. Before you post your first question, please read this short introduction guide. When posting or responding to questions please remember our values at Chandoo.org are: Humility, Passion, Fun, Awesomeness, Simplicity, Sharing Remember that we have people here for whom English is not there first language and we need to allow for this in our dealings.

    Yours,
    Chandoo
  2. Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  3. When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Sort by multiple columns

Discussion in 'VBA Macros' started by ysherriff, Mar 19, 2017 at 11:21 PM.

  1. ysherriff

    ysherriff Member

    Messages:
    235
    Hi i have the below code to sort by multiple columns but for some reason it is not working. I do not get an error message but the code doesn't work. What am i missing?

    Thanks for your help

    Code (vb):

    With ActiveSheet.Sort
                 .SortFields.Clear
                 .SortFields.Add Key:=Range("D9:D700" _
                                       ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                 .SortFields.Add Key:=Range("J9:j700" _
                                       ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                .SetRange Range("B9:N700")
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
    End With
     
  2. NARAYANK991

    NARAYANK991 Excel Ninja

    Messages:
    14,314
    Hi ,

    I have tried your code , and it works without any problem.

    Can you upload your workbook with the data in it ?

    Narayan
  3. YasserKhalil

    YasserKhalil Active Member

    Messages:
    568
    You can try this code instead
    Code (vb):
    Sub SortData()
        With ActiveSheet
            .Range("B9:N700").Sort _
                    Key1:=.Range("D9:D700"), Order1:=xlAscending, _
                    Key2:=.Range("J9:j700"), Order2:=xlDescending, _
                    Header:=xlYes
        End With
    End Sub
    Thomas Kuriakose likes this.

Share This Page