Forum Discussion

A_SIRAT's avatar
A_SIRAT
Iron Contributor
Jun 08, 2022
Solved

Automate Custom Sorting with VBA

Hi,   Please help me automate the custom sorting for the attached file. I need a VBA button to do all these (columns A: N), in this Format; The data could be 10 rows this week or even 2000 rows de...
  • Doug_Robbins_Word_MVP's avatar
    Jun 15, 2022

    A_SIRAT It turns out that using Visual Basic, a maximum of 3 levels can be sorted.  Therefore, it will be necessary to use

     

    
    Dim i As Long
    With Sheets("Raw Orders").Range("A1")
        For i = .CurrentRegion.Rows.Count - 1 To 1 Step -1
            If .Offset(i, 5) = 0 Then
                .Rows(i + 1).EntireRow.Delete
            End If
        Next i
        For i = .CurrentRegion.Rows.Count - 1 To 1 Step -1
            If .Offset(i, 6) <> "ABCD LIMITED" Then
                .Rows(i + 1).EntireRow.Delete
            End If
        Next i
        .CurrentRegion.Sort Key1:=Range("B1"), Order1:=xlAscending, _
                            Header:=xlYes
        .CurrentRegion.Sort Key1:=Range("D1"), Order1:=xlAscending, _
                            Key2:=Range("E1"), Order2:=xlAscending, _
                            Key3:=Range("A1"), Order3:=xlAscending, _
                            Header:=xlYes
    End With
    

Resources