sorting the selected cells

baha17

Board Regular
Joined
May 12, 2010
Messages
183
Hello Everyone,

I would like to create a macro on excel 2013 which sorts the selected cells. But I could not figure out how to set the key as the first column of the selected range. Just give you a clue I altered the recorded macro as below but, as you can guess the part
Code:
Key:= _
        Range("B461:B474")
should be the first column of my selected range.

thanks fort the help in advance
have a great day/night

Cheers
Baha


Code:
Sub Sort2()
    ActiveWorkbook.Worksheets("ShiftAssignments (2)").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("ShiftAssignments (2)").Sort.SortFields.Add Key:= _
        Range("B461:B474"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("ShiftAssignments (2)").Sort
        .SetRange Selection
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello Everyone,

I altered my code as
Code:
Sub Sort2()
Dim cell As Range, MergeVal$
Dim TR&, BR&, LC%, RC%
With Selection
TR = .Row
BR = .Rows.Count + .Row - 1
LC = .Column
RC = .Columns.Count + .Column - 1
Set cel = ActiveSheet.Cells(TR, LC)
Set cel2 = ActiveSheet.Cells(BR, LC)
    ActiveWorkbook.Worksheets("ShiftAssignments (2)").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("ShiftAssignments (2)").Sort.SortFields.Add Key:= _
        Range(cel.Address & ":" & cel2.Address), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
End With
    With ActiveWorkbook.Worksheets("ShiftAssignments (2)").Sort
        .SetRange Selection
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

and it works fine.
I post this in case someone needs.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

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