Wednesday, May 27, 2020

Excel VBA Sort Data Range


Sub SortDataRange()
    Dim lngLastRow As Long
 
    lngLastRow = Sheet1.Range("R10").CurrentRegion.Rows.Count + 9

    Sheet1.Sort.SortFields.Clear

    Sheet1.Sort.SortFields.Add2 Key:=Range( _
        "R10:R" & lngLastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal

    With Sheet1.Sort
        .SetRange Sheet1.Range("R10:U" & lngLastRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

No comments:

Post a Comment

Hot Topics