Sort Selected Rows by Column X-Macro?

jeffcoleky

Active Member
Joined
May 24, 2011
Messages
274
This seems like a simple question, but i don't have the answer.

I wish to Highlight any range of rows, then use a MACRO to sort it by Column "M" in Ascending order.

Can anyone help me with this please?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
What does the macro recorder give you? It can be edited to suit.

Code:
Sub Sort_TAX_P()
'
' Sort_TAX_P Macro
' sort by tax value in column P
'
' Keyboard Shortcut: Ctrl+Shift+M
'
    Rows("3492:3500").Select
    Range("E3492").Activate
    ActiveCell.Rows("1:9").EntireRow.Select
    ActiveCell.Activate
    ActiveWorkbook.Worksheets("Workbook_1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Workbook_1").Sort.SortFields.Add Key _
        :=ActiveCell.Offset(0, 11).Range("A1:A9"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Workbook_1").Sort
        .SetRange ActiveCell.Offset(0, -4).Range("A1:CL9")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

That is what I get. I would like to be able to select as many or as few rows as possible, anywhere on the spreadsheet, then sort ONLY those rows by column P. Any ideas on how to modify that?

PS. I really dont have the answer myself on how to edit this.. NO clue.
 
Last edited:
Upvote 0
I don't have Excel 2007 to test, but try:

Code:
Sub Sort_TAX_P()
'
' Sort_TAX_P Macro
' sort by tax value in column P
'
' Keyboard Shortcut: Ctrl+Shift+M
'
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Selection.EntireRow.Columns(16), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Selection.EntireRow
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
I don't have Excel 2007 to test, but try:

Code:
Sub Sort_TAX_P()
'
' Sort_TAX_P Macro
' sort by tax value in column P
'
' Keyboard Shortcut: Ctrl+Shift+M
'
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Selection.EntireRow.Columns(16), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Selection.EntireRow
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

This is Excel 2010. However, it worked great! thank you!
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
Lawrenceiow

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