Consulting

Results 1 to 5 of 5

Thread: Solved: Conditional Sorting

  1. #1
    VBAX Regular
    Joined
    Jan 2013
    Posts
    84
    Location

    Solved: Conditional Sorting

    Hi all,

    In my table, I have 6 columns (A-F). In column A I have blank cells or numbers. I need to sort this table by column B, then by C, and then by column D, but only the rows where column A has a number (more than 0) and place these rows at the top. The table Range is A4:F27.


    Here is the code for the sorting part (from a macro recording):
    [vba] Range("A4:F27").Select
    ActiveWorkbook.Worksheets("Today").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Today").Sort.SortFields.Add Key:=Range("B4:B27") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Today").Sort.SortFields.Add Key:=Range("C4:C27") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Today").Sort.SortFields.Add Key:=Range("D427") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Today").Sort
    .SetRange Range("A4:F27")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With[/vba]

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Record yourself a macro sorting the whole range on only column A. I think this will place rows without anything in column A at the bottom, keeping them in the order they were in.
    Next, select only the top part of the range and sort that as before.
    Paste the macro here and we'll tweak to automate the definition of the second range to sort.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Playing with this a bit more, rty perhaps:[VBA]Sub blah()
    With ActiveWorkbook.Worksheets("Today")
    With .Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("A5:A27"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange Range("A4:F27")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    Set SecondRange = .Range(.Range("A4"), .Range("A4").End(xlDown)).Resize(, 6)
    With .Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("B5"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.Add Key:=Range("C5"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.Add Key:=Range("D5"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange SecondRange
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    End With
    End Sub
    [/VBA]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    [QUOTE=RinghalIn my table, I have 6 columns (A-F). In column A I have blank cells or numbers. I need to sort this table by column B, then by C, and then by column D, but only the rows where column A has a number (more than 0) and place these rows at the top. The table Range is A4:F27.[/Quote]

    Since the order of operations is determined by the condition that you only sort those rows in Column A where it has a number, then Column A needs to have preference over the others. Sort by Columns A then B then C then D, rather than the logic of the recorded code in the first post.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Regular
    Joined
    Jan 2013
    Posts
    84
    Location
    Thanks Aussiebear and p45cal

    p45cal: I used your code above and changed it a little bit to work with my data. It now function as it should.
    [vba] Sub blah()

    Dim SecondRange As Range
    Dim i As Byte

    With ActiveWorkbook.Worksheets("Today")
    With .Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("A3:A27"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange Range("A4:F27")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    i = Worksheets("Today").Range("A3:A27").Cells.SpecialCells(xlCellTypeConstants) .Count + 1
    Set SecondRange = .Range(.Range("A3"), .Range("A3").End(xlDown)).Resize(i, 6)

    With .Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("B3"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.Add Key:=Range("C3"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.Add Key:=Range("D3"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange SecondRange
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    End With
    End Sub
    [/vba]

Posting Permissions

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