Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Dallas, Texas, USA
    Posts
    1,680
    Thanks
    0
    Thanked 1 Time in 1 Post

    Sort Macro (without Range.Select) (xl03)

    I think we've been down this road before, so I just spent 45 minutes using search; couldn't locate the solution, so I thought I'd re-post. I know it's possible to sort data in a table without selecting it , but I don't recall how to accomplish this. I'm going to put sort buttons at the top of each row on a 19-column data table. So I want the code to be clean and quick (and simple to input). Here is the code as it is produced by the macro recorder:

    <pre>Sub Sort_C_F()
    '
    ' Sort_C_F Macro
    ' Macro recorded 2/4/2008 by "I Forgot How To Clean The Code"

    '
    Range("datalist").Select
    Selection.Sort Key1:=Range("C8"), Order1:=xlAscending, Key2:=Range("F8") _
    , Order2:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:= _
    False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
    :=xlSortNormal
    Range("L4:R4").Select
    End Sub</pre>

    - Ricky

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Sort Macro (without Range.Select) (xl03)

    How about

    Range("datalist").Sort Key1:=Range("C8"), Order1:=xlAscending, Key2:=Range("F8") _
    , Order2:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:= _
    False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Sort Macro (without Range.Select) (xl03)

    Or equivalently but shorter

    Range("datalist").Sort Key1:=Range("C8"), Key2:=Range("F8"), Header:=xlNo

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Dallas, Texas, USA
    Posts
    1,680
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Sort Macro (without Range.Select) (xl03)

    <img src=/S/yep.gif border=0 alt=yep width=15 height=15> Thanks - I like that. I guess that the sort order is assumed to be "ascending" and that the order is only needed in the code if it is to be different?
    - Ricky

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Sort Macro (without Range.Select) (xl03)

    Yep. If you click in the word Sort in the Visual Basic Editor and press F1, you'll get the built-in help. You can expand info about the parameters; there is usually a default value that is assumed if you omit the parameter.

  6. The Following User Says Thank You to HansV For This Useful Post:

    The Hedgehog (2014-11-20)

Posting Permissions

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