Results 1 to 7 of 7

Thread: Macro To Sort Data Ascending and Decending Using VBA

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Macro To Sort Data Ascending and Decending Using VBA

    I've never done macros before, but I thought I'd ask how to do a macro to do the following:


    1. Select Columns J:AG
    2. Sort first by Column AG in ascending order(Lowest to Highest)
    3. Sort then by Clolumn J in descending order (Highest to Lowest)

    Could I just click on a button or a cell to run this macro? Thanks!

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Try this..

    Code:
    Sub ExcelFox()
        
        Dim lng As Long
        
        With Worksheets("Sheet1")
            lng = Application.Max(.Cells(.Rows.Count, "J").End(xlUp).Row, 2)
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=.Range("AG2:AG" & lng), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Sort.SortFields.Add Key:=.Range("J2:J" & lng), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            .Sort.SetRange .Range("J1:AG" & lng)
            .Sort.Header = xlYes
            .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

  3. #3
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    You can insert a shape and assign the macro to this, or call the routine from within the click event of a command button.
    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

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0
    Ok, since I'm new to this... A few questions...
    The 'macro' is called ExcelFox, right?...
    It's a module? Right? So Ican hit Alt-F11, paste this code into a Module, right?
    Then I can run it immediately or I can call it by going to 'Developer' then 'Macros', then running it, right?
    Once I run it, that's it! Right?...

    Sorry for all the 'Rights'!

    I'll try it when I get home tonight!
    Last edited by Excel Fox; 05-10-2013 at 12:25 AM. Reason: Quote Removed

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0
    Quote Originally Posted by olives View Post
    ... I'll try it when I get home tonight!
    Just tried it out, Fox! It looks good! Thanks!

    Let me ask you this... It doesn't seem to be incluidng the top row, row 1, in the sort, what do I have to change in the code to include the top row?...

    Does it have to do with this row?:

    Code:
    lng = Application.Max(.Cells(.Rows.Count, "J").End(xlUp).Row, 2)
    Is the 2 at the end what I should change to 1? Thanks!

  6. #6
    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

  7. #7
    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
  •