Results 1 to 7 of 7

Thread: Macro To Sort Data Ascending and Decending Using VBA

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    No that's not what you need to do. Here you go...

    Code:
    Sub ExcelFox()
        
        Dim lng As Long
        
        With Worksheets("Sheet1")
            lng = .Cells(.Rows.Count, "J").End(xlUp).Row
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=.Range("AG1:AG" & lng), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Sort.SortFields.Add Key:=.Range("J1:J" & lng), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            .Sort.SetRange .Range("J1:AG" & lng)
            .Sort.Header = xlNo
            .Sort.MatchCase = False
            .Sort.Orientation = xlTopToBottom
            .Sort.SortMethod = xlPinYin
            .Sort.Apply
        End With
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0
    Quote Originally Posted by Excel Fox View Post
    No that's not what you need to do. Here you go...

    Code:
    Sub ExcelFox()
        
        Dim lng As Long
        
        With Worksheets("Sheet1")
            lng = .Cells(.Rows.Count, "J").End(xlUp).Row
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=.Range("AG1:AG" & lng), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Sort.SortFields.Add Key:=.Range("J1:J" & lng), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            .Sort.SetRange .Range("J1:AG" & lng)
            .Sort.Header = xlNo
            .Sort.MatchCase = False
            .Sort.Orientation = xlTopToBottom
            .Sort.SortMethod = xlPinYin
            .Sort.Apply
        End With
        
    End Sub
    Wow! Amazing! Thanks!

Similar Threads

  1. Sort Data Using Formula To Find Top X
    By mahmoud-lee in forum Excel Help
    Replies: 12
    Last Post: 06-02-2013, 10:13 PM
  2. Sort data sheet by right_click of mouse
    By Rasm in forum Excel and VBA Tips and Tricks
    Replies: 3
    Last Post: 12-08-2012, 07:34 PM
  3. Excel Macro to Sort Data if a value changes in defined range
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 4
    Last Post: 09-05-2012, 10:31 AM
  4. Sort Data When a Header Is Clicked
    By Rasm in forum Excel Help
    Replies: 9
    Last Post: 08-01-2012, 06:46 AM
  5. Sort Worksheet by Color VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 10-25-2011, 02:25 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
  •