Results 1 to 10 of 10

Thread: Sort by row based on column value

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Aug 2017
    Posts
    17
    Location

    Sort by row based on column value

    Hello
    I'm using Excel 2010 and I would like to sort by row based on values in a column.
    So far I have

    Sub sortRow()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        Dim ViewMode As Long
        Dim i As Long
         
        With ActiveSheet
            .Select
            ViewMode = ActiveWindow.View
            ActiveWindow.View = xlNormalView
            .DisplayPageBreaks = False
             
            Firstrow = .UsedRange.Cells(1).Row + 1
            Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
             
             'We loop from Lastrow to Firstrow (bottom to top)
            For Lrow = Firstrow To Lastrow
            
                With sht.Sort
                    .SortFields.Clear
                    .SortFields.Add Key:=rng, SortOn:=xlSortOnValues, _
                       Order:=xlDescending, DataOption:=xlSortNormal
                    .SetRange rng
                    .Header = xlNo
                    .MatchCase = False
                    .Orientation = xlLeftToRight
                    .SortMethod = xlPinYin
                    .Apply
                End With
            Next Lrow
        End With
    End Sub
    I believe the problem is at the .SetRange rng not sure what to put if I want column B for example
    I tried .setrange :=(B:B) but it errors
    I should mention I wish to order the row in descending order based on the column word
    Last edited by strato; 08-09-2017 at 11:29 AM.

Posting Permissions

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