+ Reply to Thread
Results 1 to 2 of 2

Are these 2 pieces of sorting code return the same result?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2012
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    15

    Are these 2 pieces of sorting code return the same result?

    I recorded the macro and get this code:
    
    ActiveWorkbook.Worksheets("PM Close").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("PM Close").Sort.SortFields.Add Key:=Range( _
            "H2:H991"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.Worksheets("PM Close").Sort.SortFields.Add Key:=Range( _
            "G2:G991"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("PM Close").Sort
            .SetRange Range("A1:P991")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    If I change it to
    ActiveWorkbook.Worksheets("PM Close").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("PM Close").Sort.SortFields.Add Key:=Range( _
            "H:H"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.Worksheets("PM Close").Sort.SortFields.Add Key:=Range( _
            "G:G"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("PM Close").Sort
            .SetRange Range("A1:P991")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    As you can see H2:H991 now is H:H and G2:G991 become G:H
    Because I want to re-use this code for flexible row counts (the range I can replace by string later), but I wonder that the column range I change above, will or are they the same? I mean same sort result?

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Are these 2 pieces of sorting code return the same result?

    Create a copy of your workbook and test both. That will tell you if they produce the same results.

    You could also create dynamically named ranges and use those in place of the column letters. (Search for dynamic named range to see how to create one.)

+ 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