+ Reply to Thread
Results 1 to 6 of 6

Using sort descending in VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-18-2009
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2003
    Posts
    104

    Using sort descending in VBA

    I have some sheets which I would like to loop through and use sort descending on column S, would somebody help me out with the code to do so? I actually have the looping code already, I just need the sort descending bit.
    Last edited by eonizuka; 01-05-2010 at 02:46 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using sort descending in VBA

    Try recording a macro?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    03-18-2009
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2003
    Posts
    104

    Re: Using sort descending in VBA

    I used the macro recorder and inserted the recorded code into the macro I'm using. I'm getting an error on the .sort section of the code stating "method or data member" not found. I'm not sure of the syntax to point to the current worksheet either. Thanks for your patience with me on this. Edit: I removed the (s) from the worksheet object and the code runs but it isn't sorting the data.

    
    
    Sub x()
    
    Dim n As Long, ws As Worksheet
    
    
      Dim lrow As Long, sh As Worksheet
        Application.ScreenUpdating = False
        On Error Resume Next
    
    worksheetcount = ActiveWorkbook.Worksheets.Count
       
    beginningrow = worksheetcount - 2
    
    For i = beginningrow To Worksheets.Count
        With Worksheets(i)
    
            n = .Range("Q" & Rows.Count).End(xlUp).Row
            .Range("S2:S" & n).Formula = "=(Q2-T2)/V2"
            .Range("T2:T" & n).Formula = "=AVERAGE(Q$2:Q$" & n & ")"
            .Range("U2:U" & n).Formula = "=MEDIAN(Q$2:Q$" & n & ")"
            .Range("V2:V" & n).Formula = "=STDEV(Q$2:Q$" & n & ")"
            .Range("W2:W" & n).Formula = "=SKEW(Q$2:Q$" & n & ")"
            .Range("T2:W" & n).Value = .Range("T2:W" & n).Value
        End With
        
        Columns("S:S").Select
        ActiveWorkbook.Worksheet.sort.SortFields.Clear
        ActiveWorkbook.Worksheet.sort.SortFields.Add Key:= _
            Range("S1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheet.sort
            .SetRange Range("A2:W11")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next i
    
    
    End Sub
    Last edited by eonizuka; 01-05-2010 at 01:15 PM.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using sort descending in VBA

    Untested (I edited this in 2003). Your references were not qualified to the worksheet. The most important line in the code is the very first one.
    Option Explicit
    
    Sub x()
        Dim n           As Long
        Dim i           As Long
    
        Application.ScreenUpdating = False
    
        With ActiveWorkbook
            For i = Worksheets.Count - 2 To Worksheets.Count
                With Worksheets(i)
                    n = .Range("Q" & .Rows.Count).End(xlUp).Row
                    .Range("S2:S" & n).Formula = "=(Q2-T2)/V2"
                    .Range("T2:T" & n).Formula = "=AVERAGE(Q$2:Q$" & n & ")"
                    .Range("U2:U" & n).Formula = "=MEDIAN(Q$2:Q$" & n & ")"
                    .Range("V2:V" & n).Formula = "=STDEV(Q$2:Q$" & n & ")"
                    .Range("W2:W" & n).Formula = "=SKEW(Q$2:Q$" & n & ")"
                    .Range("T2:W" & n).Value = .Range("T2:W" & n).Value
    
                    .Sort.SortFields.Add Key:=.Range("S2"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                    .Sort.SetRange .Range("A2:W11")
                    
                    With .Sort
                        .SortFields.Clear
                        .Orientation = xlTopToBottom
                        .Header = xlNo
                        .MatchCase = False
                        .Apply
                    End With
                End With
            Next i
        End With
    End Sub

  5. #5
    Forum Contributor
    Join Date
    03-18-2009
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2003
    Posts
    104

    Re: Using sort descending in VBA

    The code isn't sorting the data; I have attached a sample workbook with the data and code. I slightly modified it for use with just this one sheet.
    Attached Files Attached Files

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using sort descending in VBA

    Konnichi wa Eonizuka san,

    Akemashite omedetō gozaimasu!

    I can't edit Excel 2007 files, only read them. So, the modifications to your macro are untested, but it should work. Here is the modified macro.
    Sub x()
    
      Dim n As Long, ws As Worksheet
      Dim lrow As Long, sh As Worksheet
      
        Application.ScreenUpdating = False
        On Error Resume Next
    
        worksheetcount = ActiveWorkbook.Worksheets.Count
       
        beginningrow = worksheetcount - 2
    
        For i = beginningrow To Worksheets.Count
          With Worksheets(i)
            n = .Range("Q" & Rows.Count).End(xlUp).Row
            .Range("S2:S" & n).Formula = "=(Q2-T2)/V2"
            .Range("T2:T" & n).Formula = "=AVERAGE(Q$2:Q$" & n & ")"
            .Range("U2:U" & n).Formula = "=MEDIAN(Q$2:Q$" & n & ")"
            .Range("V2:V" & n).Formula = "=STDEV(Q$2:Q$" & n & ")"
            .Range("W2:W" & n).Formula = "=SKEW(Q$2:Q$" & n & ")"
            .Range("T2:W" & n).Value = .Range("T2:W" & n).Value
          End With
        
          With Worksheets(i).Sort
            .SortFields.Clear
            .SortFields.Add Key:=Worksheets(i).Range("S1"), SortOn:=xlSortOnValues, _
                            Order:=xlDescending, DataOption:=xlSortNormal
            .SetRange Worksheets(i).Range("A2:W11")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
          End With
        Next i
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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