Sort With Selection

flds

Board Regular
Joined
Jun 19, 2008
Messages
73
Hi

I am trying to modify this recorded code. I tried, but failed.

In an active worksheet, I am trying to select a few rows and want to sort them by column "K"

I would appreciate if someone can modify this for me.

Thanks
FLDS

Code: I recorded

Sub Sort2()
'
' Sort2 Macro
'

With Selection 'I added this row
' Rows("29:54").Select 'I stopped this row
ActiveWorkbook.Worksheets("Record").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Record").Sort.SortFields.Add Key:=Range("K29:K54"), _ 'I want to select the range
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Record").Sort
.SetRange Range("A29:X54") 'I want to select the rows
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With 'I added this row
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
Code:
Sub Sort2()
'
' Sort2 Macro
'

With Selection
   ActiveSheet.Sort.SortFields.Clear
   ActiveSheet.Sort.SortFields.Add key:=Range("K" & Selection.Row), _
   SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
   With ActiveSheet.Sort
      .SetRange Selection
      .header = xlNo
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
End With
End Sub
 
Upvote 0
Hi Fluff,

Thank you, that was quick. you made my day.

Appreciate your time.

FLDS



How about
Code:
Sub Sort2()
'
' Sort2 Macro
'

With Selection
   ActiveSheet.Sort.SortFields.Clear
   ActiveSheet.Sort.SortFields.Add key:=Range("K" & Selection.Row), _
   SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
   With ActiveSheet.Sort
      .SetRange Selection
      .header = xlNo
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
End With
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Another way:

Code:
Sub Sort2()
  With ActiveWindow.RangeSelection
    .Sort Key1:=Intersect(.Worksheet.Range("K:K"), .Cells), Header:=xlNo
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top