Jun 07 2022 07:08 PM
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 depending on the orders.
1. Delete orders lines that have zero quantities in column F.
2. Delete all order lines EXCEPT for ABCD LIMITED.
2. Custom Sort First by Column D then by E (Order A to Z).
3.The Custom sort again all data by Columns H then by L (Order A to Z)
Jun 07 2022 11:42 PM
@A_SIRAT A macro containing the following code will delete the unwanted rows
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
End With
I am not sure of the two sorts that you want. Is something to be done with the data following the first sort and before the second is performed?
Jun 09 2022 09:25 AM
Jun 09 2022 12:30 PM
@A_SIRAT Is anything to be done between sorts 1 and 2?
Or, is it just a single sort First by Column D, then by E and then by H and then by L?
Jun 12 2022 07:28 PM
Jun 12 2022 08:05 PM
Jun 14 2022 02:36 AM
I do not get the same results when doing the sorting in two steps. I have attached an example file of how I would like to have it. Hope this is clear.
Jun 14 2022 03:13 AM
Jun 14 2022 03:57 AM
Jun 14 2022 05:33 AM
@A_SIRAT For the column layout in the workbook that you provided, the complete code would be
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("D1"), Order1:=xlAscending, _
Key2:=Range("E1"), Order2:=xlAscending, _
Key3:=Range("A1"), Order3:=xlAscending, _
Key4:=Range("B1"), Order4:=xlAscending, _
Header:=xlYes
End With
Jun 14 2022 08:59 AM
It does the custom sorting but gives this error afterwards. Please check and advise.
Thanks in advance.
Jun 14 2022 06:27 PM
Jun 14 2022 07:46 PM
Solution@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
Jun 14 2022 08:35 PM
Jun 14 2022 07:46 PM
Solution@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