Forum Discussion

AKuma0411's avatar
AKuma0411
Brass Contributor
Nov 20, 2024
Solved

Macro to sort data

Hello,

I want help with macro code: Here's the macro I'm using:

Sub ClearAndSortDataAutomatically()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Review") ' Ensure this matches your actual sheet name

    ' Clear existing sort settings
    ws.Sort.SortFields.Clear

    ' Unmerge any merged cells in the specified range if necessary
    ws.Range("A8:P84").UnMerge ' Adjust the range as needed

    ' Perform the sort specifically for the range K9 to K84
    With ws.Sort
        ' Sort by Segment (Column F) Z to A
        .SortFields.Add Key:=ws.Range("F8:F84"), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        
        ' Sort by Indicator (Column E) A to Z
        .SortFields.Add Key:=ws.Range("E8:E84"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        
        ' Sort by Total Procedure (Column P) Largest to Smallest
        .SortFields.Add Key:=ws.Range("P8:P84"), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        
        ' Sort by Set# (Column K) A to Z
        .SortFields.Add Key:=ws.Range("K8:K84"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        
        ' Perform the actual sort limited to rows 9 through 84
        .SetRange ws.Range("A8:P84") ' Adjust range as needed within the specified limits
        .Header = xlYes ' Indicates that the first row is a header
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    ' After sorting, merge the cells from A9 to A44
    ws.Range("A9:A44").Merge ' Merge the specified range
End Sub

Here's what I want to achieve in excel 

  1. Segment first in Z to A order
  2. Indicator  A to Z
  3. total procedures Largest to smallest 
  4. Set # Z to A  

    Please note: My data starts row 9 and column A9 to A44 is merged cell, also, I have column G to Column J hidden, column N&O are hidden. I have attached excel wb where one is before sorting how the data looks and other tab is after sorting how the data should look like. appreciate any help, I'm open to any other suggestions as well

  • The problem is that the Set# column contains some numbers, some pure text values and some text values that look like numbers. This is the nearest I can get:

    Sub ClearAndSortDataAutomatically()
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets("Review") ' Ensure this matches your actual sheet name
    
        ' Unmerge any merged cells in the specified range if necessary
        'ws.Range("A8:P84").UnMerge ' Adjust the range as needed
    
        ' Perform the sort specifically for the range K9 to K84
        With ws.Sort
            ' Clear existing sort settings
            .SortFields.Clear
    
            ' Sort by Segment (Column F) Z to A
            .SortFields.Add Key:=ws.Range("F8:F84"), _
                SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    
            ' Sort by Indicator (Column E) A to Z
            .SortFields.Add Key:=ws.Range("E8:E84"), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
            ' Sort by Total Procedure (Column P) Largest to Smallest
            .SortFields.Add Key:=ws.Range("P8:P84"), _
                SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    
            ' Sort by Set# (Column K) A to Z
            .SortFields.Add Key:=ws.Range("K8:K84"), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    
            ' Perform the actual sort limited to rows 9 through 84
            .SetRange ws.Range("B8:P84") ' Adjust range as needed within the specified limits
            .Header = xlYes ' Indicates that the first row is a header
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
        ' After sorting, merge the cells from A9 to A44
        'ws.Range("A9:A44").Merge ' Merge the specified range
    End Sub

     

1 Reply

  • The problem is that the Set# column contains some numbers, some pure text values and some text values that look like numbers. This is the nearest I can get:

    Sub ClearAndSortDataAutomatically()
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets("Review") ' Ensure this matches your actual sheet name
    
        ' Unmerge any merged cells in the specified range if necessary
        'ws.Range("A8:P84").UnMerge ' Adjust the range as needed
    
        ' Perform the sort specifically for the range K9 to K84
        With ws.Sort
            ' Clear existing sort settings
            .SortFields.Clear
    
            ' Sort by Segment (Column F) Z to A
            .SortFields.Add Key:=ws.Range("F8:F84"), _
                SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    
            ' Sort by Indicator (Column E) A to Z
            .SortFields.Add Key:=ws.Range("E8:E84"), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
            ' Sort by Total Procedure (Column P) Largest to Smallest
            .SortFields.Add Key:=ws.Range("P8:P84"), _
                SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    
            ' Sort by Set# (Column K) A to Z
            .SortFields.Add Key:=ws.Range("K8:K84"), _
                SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    
            ' Perform the actual sort limited to rows 9 through 84
            .SetRange ws.Range("B8:P84") ' Adjust range as needed within the specified limits
            .Header = xlYes ' Indicates that the first row is a header
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
        ' After sorting, merge the cells from A9 to A44
        'ws.Range("A9:A44").Merge ' Merge the specified range
    End Sub

     

Resources