Sort data A-Z

ngocanh87

Board Regular
Joined
Mar 16, 2016
Messages
85
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello all,

I want sort by M4 & D4 to A-Z as (1) but how can get DLVS on top then DLV (2). Thank all
Sub SCN001()
Range("A4:AZ5").AutoFilter Field:=8, Criteria1:=""
Range("A4:AZ500").Sort Key1:=Range("M4"), Key2:=Range("D4"), Header:=xlYes, Order1:=xlAscending, Order2:=xlAscending End Sub
(1)
SCN001(Delivery).xls
DM
4Mã phiếu giao vậnKhách hàng
5DLV-20230329-031C00039
6DLV-20230329-032C00039
7DLV-20230329-032C00039
8DLV-20230329-032C00039
9DLV-20230329-052C00144
10DLV-20230329-052C00144
11DLV-20230329-053C00144
12DLV-20230329-038C00144-01
13DLV-20230329-039C00144-01
14DLV-20230329-040C00144-01
15DLV-20230329-041C00144-01
16DLV-20230329-041C00144-01
17DLV-20230329-042C00144-01
18DLV-20230329-043C00144-01
19DLV-20230329-044C00144-01
20DLV-20230329-045C00144-01
21DLV-20230329-046C00144-01
22DLV-20230329-047C00144-01
23DLV-20230329-048C00144-01
24DLV-20230329-034C00337
25DLV-20230329-034C00337
26DLV-20230329-034C00337
27DLV-20230329-034C00337
28DLV-20230329-035C00337
29DLV-20230329-035C00337
30DLV-20230329-035C00337
31DLV-20230329-035C00337
32DLV-20230329-055C00388
33DLV-20230329-056C00388
34DLV-20230329-056C00388
35DLV-20230329-057C00388
36DLV-20230329-058C00388
37DLV-20230329-058C00388
38DLV-20230329-059C00388
39DLV-20230329-060C00388
40DLV-20230329-061C00388
41DLV-20230329-061C00388
42DLV-20230329-062C00388
43DLV-20230329-063C00388
44DLV-20230329-064C00388
45DLV-20230329-065C00388
46DLV-20230329-066C00388
47DLV-20230329-067C00388
48DLV-20230329-068C00388
49DLVS-20230329-004C00392
50DLVS-20230329-006C00392
51DLV-20230329-033C00677
52DLV-20230329-069C00846
53DLV-20230329-069C00846
54DLV-20230329-069C00846
55DLV-20230329-069C00846
56DLV-20230329-069C00846
57DLV-20230329-069C00846
58DLV-20230329-069C00846
59DLV-20230329-069C00846
60DLV-20230329-069C00846
61DLV-20230329-070C00846
62DLV-20230329-070C00846
63DLV-20230329-070C00846
64DLV-20230329-070C00846
65DLV-20230329-070C00846
66DLV-20230329-070C00846
67DLV-20230329-070C00846
68DLV-20230329-070C00846
69DLV-20230329-070C00846
70DLV-20230329-070C00846
71DLV-20230329-070C00846
72DLVS-20230329-002C01021-01
73DLVS-20230329-003C01021-01
74DLV-20230329-026C01065
75DLV-20230329-026C01065
76DLVS-20230329-001C01213
77DLV-20230329-018C01272
78DLV-20230329-019C01272
79DLV-20230329-036C01289
80DLVS-20230329-005C01302
SCN001


(2)
SCN001(Delivery).xls
DE
2Mã phiếu giao vậnKhách hàng
3DLVS-20230329-004C00392
4DLVS-20230329-006C00392
5DLVS-20230329-002C01021-01
6DLVS-20230329-003C01021-01
7DLVS-20230329-001C01213
8DLVS-20230329-005C01302
9DLV-20230329-031C00039
10DLV-20230329-032C00039
11DLV-20230329-032C00039
12DLV-20230329-032C00039
13DLV-20230329-052C00144
14DLV-20230329-052C00144
15DLV-20230329-053C00144
16DLV-20230329-038C00144-01
17DLV-20230329-039C00144-01
18DLV-20230329-040C00144-01
19DLV-20230329-041C00144-01
20DLV-20230329-041C00144-01
21DLV-20230329-042C00144-01
22DLV-20230329-043C00144-01
23DLV-20230329-044C00144-01
24DLV-20230329-045C00144-01
25DLV-20230329-046C00144-01
26DLV-20230329-047C00144-01
27DLV-20230329-048C00144-01
28DLV-20230329-034C00337
29DLV-20230329-034C00337
30DLV-20230329-034C00337
31DLV-20230329-034C00337
32DLV-20230329-035C00337
33DLV-20230329-035C00337
34DLV-20230329-035C00337
35DLV-20230329-035C00337
36DLV-20230329-055C00388
37DLV-20230329-056C00388
38DLV-20230329-056C00388
39DLV-20230329-057C00388
40DLV-20230329-058C00388
41DLV-20230329-058C00388
42DLV-20230329-059C00388
43DLV-20230329-060C00388
44DLV-20230329-061C00388
45DLV-20230329-061C00388
46DLV-20230329-062C00388
47DLV-20230329-063C00388
48DLV-20230329-064C00388
49DLV-20230329-065C00388
50DLV-20230329-066C00388
51DLV-20230329-067C00388
52DLV-20230329-068C00388
53DLV-20230329-033C00677
54DLV-20230329-069C00846
55DLV-20230329-069C00846
56DLV-20230329-069C00846
57DLV-20230329-069C00846
58DLV-20230329-069C00846
59DLV-20230329-069C00846
60DLV-20230329-069C00846
61DLV-20230329-069C00846
62DLV-20230329-069C00846
63DLV-20230329-070C00846
64DLV-20230329-070C00846
65DLV-20230329-070C00846
66DLV-20230329-070C00846
67DLV-20230329-070C00846
68DLV-20230329-070C00846
69DLV-20230329-070C00846
70DLV-20230329-070C00846
71DLV-20230329-070C00846
72DLV-20230329-070C00846
73DLV-20230329-070C00846
74DLV-20230329-026C01065
75DLV-20230329-026C01065
76DLV-20230329-018C01272
77DLV-20230329-019C01272
78DLV-20230329-036C01289
Sheet1
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Perhaps pull off the prefix into a helper column like this:
Excel Formula:
=LEFT(D5,FIND("-",D5)-1)
and use that in your primary sort, and then your other two the second and third sort fields.
 
Upvote 0
Solution
Hi, thanks

Sub SCN001()
Dim lastrow As Long
lastrow = Cells(Rows.Count, 4).End(xlUp).Row

Range("A4:AZ5").AutoFilter Field:=8, Criteria1:=""
Range("E5:E" & lastrow).NumberFormat = "General"
Range("E5:E" & lastrow).Formula = "=LEFT(D5,FIND(""-"",D5)-1)"

Range("A4:AZ500").Sort Key1:=Range("E4"), Key2:=Range("D4"), Key3:=Range("M4"), Header:=xlYes, Order1:=xlDescending, Order2:=xlAscending, Order3:=xlAscending

End Sub
 
Upvote 0
If you are using VBA and don't want to use a helper column, another possibility is to use the range replace method.
VBA Code:
Sub SCN002()
    Dim lastrow As Long
    lastrow = Cells(Rows.Count, 4).End(xlUp).Row

    Range("A4:AZ5").AutoFilter Field:=8, Criteria1:=""
    Range("D5:D" & lastrow).Replace What:="DLVS", Replacement:="ALVS", LookAt:=xlPart
    Range("A4:AZ500").Sort Key1:=Range("D4"), Key2:=Range("M4"), Header:=xlYes, Order1:=xlAscending, Order2:=xlAscending
    Range("D5:D" & lastrow).Replace What:="ALVS", Replacement:="DLVS", LookAt:=xlPart
End Sub
 
Upvote 0
Thank you, but M4 Customer code not to A-Z (smallest-biggest).
 

Attachments

  • 2023-03-31_10-27-07.jpg
    2023-03-31_10-27-07.jpg
    105.6 KB · Views: 4
Upvote 0
One way with helper column:

try to combine "A" (or "Z") with text in column M:

=IF(SEARCH("-",D5)=5,"A","Z")&M5

or

=IF(MID(D5,4,1)="S","A","Z")&M5

then filter this column.

(P/S: Nếu muốn dùng tiếng Việt thì vô đây nhé www.giaiphapexcell.com, nick của mình cũng vậy)
 
Upvote 0
Thank you, but M4 Customer code not to A-Z (smallest-biggest).
VBA Code:
Sub SCN002()
    Dim WS As Worksheet
    Dim ColRange As Range

    Set WS = ActiveSheet
    Set ColRange = WS.Range("D5", WS.Range("D" & WS.Rows.Count).End(xlUp))

    WS.AutoFilterMode = False
    ColRange.Replace What:="DLVS", Replacement:="ALVS", LookAt:=xlPart
    With WS.Range("A4:AZ500")
        .Sort Key1:=Range("D4"), Key2:=Range("M4"), Header:=xlYes, Order1:=xlAscending, Order2:=xlAscending
        .AutoFilter Field:=4, Criteria1:="=ALVS*"
        With Application.Intersect(WS.UsedRange, .SpecialCells(xlCellTypeVisible))
            .Sort Key1:=Range("M4"), Key2:=Range("D4"), Header:=xlYes, Order1:=xlAscending, Order2:=xlAscending
        End With
        .AutoFilter Field:=4, Criteria1:="=DLV-*"
        With Application.Intersect(WS.UsedRange, .SpecialCells(xlCellTypeVisible), .Offset(1))
            .Sort Key1:=Range("M4"), Key2:=Range("D4"), Header:=xlNo, Order1:=xlAscending, Order2:=xlAscending
        End With
    End With
    WS.AutoFilter.ShowAllData
    ColRange.Replace What:="ALVS", Replacement:="DLVS", LookAt:=xlPart
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top