View Single Post
 
Old 05-25-2023, 02:21 PM
14spar15 14spar15 is offline Windows 7 64bit Office 2010 64bit
Competent Performer
 
Join Date: Mar 2011
Posts: 100
14spar15 is on a distinguished road
Default Sort selected rows

Hello, I select consecutive rows on a sheet and run this code to sort A>Z for those selected rows but it only works correctly with 4 rows. I want to be able to select more or in some cases fewer than 4 rows to be sorted. The selected rows will always start as consecutive rows on the sheet. Thanks

Code:
Sub Sort()

    ActiveWorkbook.Worksheets("EnterSheet").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("EnterSheet").Sort.SortFields.Add Key:=ActiveCell. _
        Offset(0, 5).Range("A1:A4"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("EnterSheet").Sort
        .SetRange ActiveCell.Range("A1:AK4")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
I have added a sample attachment.It seems the range needs to be changed.
Attached Files
File Type: xlsm Book3V.xlsm (17.9 KB, 3 views)

Last edited by 14spar15; 05-26-2023 at 08:27 AM. Reason: Added sample attachment
Reply With Quote