Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 2 of 2

Thread: Sort on the last column

  1. #1
    New Member
    Join Date
    Mar 2002
    Posts
    13
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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. #2
    MrExcel MVP Jay Petrulis's Avatar
    Join Date
    Mar 2002
    Location
    Chicago, IL USA
    Posts
    2,040
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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
    '------

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •