Consulting

Results 1 to 6 of 6

Thread: Solved: How to do sorting in a noncontiguous selection

  1. #1

    Solved: How to do sorting in a noncontiguous selection

    Hi all,

    I'm not sure how to do the sorting in a noncontiguous selection which has multiple areas.

    Example

    1
    2
    4
    5
    3


    1
    3
    2


    1
    6
    4

    There are 3 areas here, and I want to sort them seperately, the results will look like:

    1
    2
    3
    4
    5


    1
    2
    3


    1
    4
    6

    Any quick way to write in VBA? Please advise, thanks a lot!!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Snoopies.
    Try recoding a macro to carry out your sort. You should end up with something like the following (Spaces added for clarity)
    [vba]
    Sub Macro2()
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

    Selection.End(xlDown).Select
    Selection.End(xlDown).Select

    Range(Selection, Selection.End(xlDown)).Select
    Selection.Sort Key1:=Range("A11"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

    Selection.End(xlDown).Select
    Selection.End(xlDown).Select

    Range(Selection, Selection.End(xlDown)).Select
    Selection.Sort Key1:=Range("A21"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

    Range("A1").Select
    End Sub
    [/vba]
    Examine the code, find the repetition and the variances and you get

    [vba]
    Sub Macro2A()
    Range("A1").Select 'Line added
    'Add a loop
    Do
    Range(Selection, Selection.End(xlDown)).Select
    'Fix sort key
    Selection.Sort Key1:=ActiveCell, Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

    Selection.End(xlDown).Select
    Selection.End(xlDown).Select
    'Find some way to finish
    Loop Until ActiveCell.Row = Cells.Rows.Count

    Range("A1").Select
    End Sub

    [/vba]
    With a bit of work, this can be improved further, but the basics are there to sort any number of ranges in column A.

    BTW if your Excel is prior to 2003, delete this bit from my code wherever it occurs ", _
    DataOption1:=xlSortNormal"
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Wonderful! Thank you so much!

  4. #4
    Just one more question ...

    How can I modify it if I want to sort not only column A, but the entirerow?
    Thanks!

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    Change the first line of the Do Loop that Malcolm gave you to:

    [vba]Range(Selection.EntireRow, Selection.End(xlDown).EntireRow).Select[/vba]

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    THANK YOU !!

Posting Permissions

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