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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
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,226,117
Messages
6,189,061
Members
453,524
Latest member
AshJames

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