Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-25-2023, 02:21 PM
14spar15 14spar15 is offline Sort selected rows Windows 7 64bit Sort selected rows Office 2010 64bit
Advanced Beginner
Sort selected rows
 
Join Date: Mar 2011
Posts: 97
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, 1 views)

Last edited by 14spar15; 05-26-2023 at 08:27 AM. Reason: Added sample attachment
Reply With Quote
  #2  
Old 05-26-2023, 08:57 AM
p45cal's Avatar
p45cal p45cal is online now Sort selected rows Windows 10 Sort selected rows Office 2019
Expert
 
Join Date: Apr 2014
Posts: 867
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

try:
Code:
Sub Sort2()
Set SortRng = Intersect(Selection.EntireRow, Columns("A:AK"))
If Not SortRng Is Nothing Then SortRng.Sort Columns("F"), xlAscending, Header:=xlNo
End Sub
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort selected rows Copy and Paste certain columns from selected rows. 14spar15 Excel Programming 2 05-18-2023 07:57 AM
Collapse Rows When NOT Selected sheridan60 Excel 1 11-30-2022 01:23 AM
Sort selected rows Set Range based on selected rows. 14spar15 Excel Programming 8 11-19-2018 08:08 AM
Sort selected rows Selecting values from different rows in a table and sort them in order in a single row FromF Excel 2 09-09-2014 02:30 AM
Trying to autofilter 8-10 rows of numbers to sort in a group Jack-P-Winner Excel 0 11-25-2013 10:55 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:48 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft