+ Reply to Thread
Results 1 to 9 of 9

Code To Sort Specific Range

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Code To Sort Specific Range

    I need to sort from the bottom row of the spreadhseet up to and including row 5 by the values in column X Ascending order could someone give me a piece of code that would do this please?
    Last edited by timbo1957; 10-10-2011 at 05:49 AM.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Sort Specific Range

    Morning,

    Can't you just click on the column and sort Z>A?

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Code To Sort Specific Range

    No that will include the header rows, I need something will only sort from row 5 down on a spreadhseet that is constantly changing length, hence from the bottom up to row 5.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Sort Specific Range

    How may columns do you have?

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Code To Sort Specific Range

    125 columns in total, rows currently 309 (that includes the first 4 rows I don't want sorted)

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code To Sort Specific Range

    Try something like this:

    Public Sub sortAsc()
    Dim lr As Long
    
    Sheet1.AutoFilterMode = False
    lr = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
    
    With Sheet1.Range("A5:O" & lr)
        .AutoFilter
        .sort Key1:=Range("D5"), Order1:=xlAscending
    End With
    
    Sheet1.AutoFilterMode = False
    
    End Sub

  7. #7
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Code To Sort Specific Range

    Hi Kyle123

    That doesn't work, I have changed the ranges to suit as follows -

       Dim lr As Long
    
        Sheet1.AutoFilterMode = False
        lr = Sheet1.Cells(Sheet1.Rows.Count, "B").End(xlUp).Row ' Changed to B Column A is blank
    
        With Sheet1.Range("A5:DU" & lr) 'Expanded to Column DU
            .AutoFilter
            .Sort Key1:=Range("X5"), Order1:=xlAscending 'Changed to Ascending
        End With
    
        Sheet1.AutoFilterMode = False

  8. #8
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Code To Sort Specific Range

    hi, timbo1957, try this option:

    Sub test()
    
    Application.ScreenUpdating = 0
    
    With Range("a5", Cells(Rows.Count, "du").End(xlUp))
    
        .Sort key1:=Range("x5"), order1:=xlAscending, header:=xlNo
    
    End With
    
    Application.ScreenUpdating = 1
    
    End Sub
    amend last row definition to your actual data layout

  9. #9
    Valued Forum Contributor
    Join Date
    11-15-2007
    Location
    England
    MS-Off Ver
    Office Pro Plus 2021
    Posts
    424

    Re: Code To Sort Specific Range

    Thanks watersev, worked a treat!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1