• 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...

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

Is the ‘Sorting’ code used below is correct way to do

ThrottleWorks

Excel Ninja
Hi,
I am using below mentioned code to sort data. I am not facing any issue with this code.

However I wanted to know if below code is correct, in other words, I want to make sure, code will not generate erroneous result.

So that I can repeat same code elsewhere without any worry.
I had checked this code with dummy data several times and did not found any issue.

Could you please review if you get time.
Code:
Sub Process_Raw_Data()
    Dim TempRng As Range
    Dim TempLr As Long
   
    'Sheet Name and Column Position is already changed to suit below code
    TempLr = RawSht.Cells(RawSht.Rows.Count, 1).End(xlUp).Row
    Set TempRng = RawSht.Range(RawSht.Cells(1, 1), RawSht.Cells(TempLr, 37))
   
    'Sort Data
    ToolData.Worksheets("Sheet1").Sort.SortFields.Clear
    ToolData.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("I2:I" & TempLr) _
    , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
   
    With ToolData.Worksheets("Sheet1").Sort
        .SetRange TempRng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
You can use simple one liner:

Code:
Range("A1:D10").Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("B1"), Order2:=xlAscending, Header:=xlYes
 
Back
Top