Consulting

Results 1 to 5 of 5

Thread: Sort each group in descending order

  1. #1

    Question Sort each group in descending order

    So what i basically want is to sort these data independently from one another from highest to lowest. Columns A through F, sorted based on column F, group H to M sorted based on column M and so on, I would like the VBA not to have a limit because my data does not always end in the same column. Any help would be appreciated.

    Thanks in advance.Sample.xlsx

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Again, this is very similar to your other questions

    You can integrate each of the small macros into a larger single one


    Option Explicit
    Sub SortData()
        Dim i As Long, iLast As Long, j As Long
        Dim r As Range
         
        Application.ScreenUpdating = False
         
        With Worksheets("Data")
            iLast = .Cells(1, .Columns.Count).End(xlToLeft).Column
             
            For i = 1 To iLast Step 5
                Set r = .Cells(1, i).CurrentRegion
                             
                With .Sort
                    .SortFields.Clear
                    .SortFields.Add Key:=r.Columns(6), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                    .SetRange r
                    .Header = xlGuess
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
            End With
                 
            Next i
             
        End With
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Sub SameQuestionAsInOtherThreadSort()
        Dim R As Range, CellGroup As Range
        Dim CollectionOfRanges As Areas
    
        With ActiveSheet
            Set CollectionOfRanges = Rows("1:" & .Rows.Count).SpecialCells(xlCellTypeConstants).Areas
            
            For Each CellGroup In CollectionOfRanges
                CellGroup.Sort Key1:=CellGroup.Range("A4"), Order1:=xlDescending
            Next CellGroup
        End With
    End Sub

  4. #4
    Thanks guys, you guys made my weekend.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sufficient code
    Sub M_snb() 
      For Each it in activesheet.rows(1).specialcells(2).areas 
        it.currentregion.sort it.cells(1) 
      Next 
    End Sub

Posting Permissions

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