![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Posts: 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 |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
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 '------ |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|