+ Reply to Thread
Results 1 to 6 of 6

problem when sorting code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-06-2013
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    193

    Lightbulb problem when sorting code

    when i am trying to sort range N:O
    N1 and O1 have headers when i am using this comment
    it sorted the entire range means the heading is also sorted

    Sub test()
    Columns("n:o").Select
    Selection.Sort Key1:=Range("n2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

    End Sub

    anyone correct this?

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: problem when sorting code

    Sort is member of Worksheet, not range.

    Try this.

    Sub Macro1()
    
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("N:O")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  3. #3
    Registered User
    Join Date
    07-10-2013
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2007
    Posts
    40

    Re: problem when sorting code

    try this

    Sub Macro1()
        Range("N1").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("N2:N11") _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("O2:O11") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("N1:O11")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub

  4. #4
    Forum Contributor
    Join Date
    04-06-2013
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    193

    Re: problem when sorting code

    i use this way to solve
    Range("N2:O100000").Select '100000 for sample
    Selection.Sort Key1:=Range("n2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

  5. #5
    Registered User
    Join Date
    07-10-2013
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2007
    Posts
    40

    Re: problem when sorting code

    use Header:=xlNo
    and remove OrderCustom:=1

    then run the code.

  6. #6
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: problem when sorting code

    Glad that you solved this...

+ 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