Sort on the last column

Wrecker

New Member
Joined
Mar 3, 2002
Messages
13
Can anyone please tell me the code to sort on the last column in a range? I have the range selected, But I don't know the code for " sort on the last column" .

Thanks
Wrecker
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,

I saw a more elegant solution offered on the board the last few days, but cannot find the link.

Anyway, this sorts the selected area ascending with the last column as the only criteria level. Change as needed.

'------
Sub test()
Dim FirstRngCol As Integer
Dim FirstRngRow As Long
Dim ColCount As Integer
Dim RowCount As Long
Dim SortStart As String

RowCount = Selection.Rows.Count
If RowCount = 1 Then Exit Sub

ColCount = Selection.Columns.Count
FirstRngCol = Selection.Column
FirstRngRow = Selection.Row

SortStart = Cells(FirstRngRow, FirstRngCol).Offset(0, ColCount - 1).Address
With Selection
Range(SortStart).Sort Key1:=Range(SortStart), Order1:=xlAscending, Header:=xlNo
End With

End Sub
'------
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,243
Members
448,555
Latest member
RobertJones1986

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