Multifaceted Sorting

mharper90

Board Regular
Joined
May 28, 2013
Messages
117
Office Version
  1. 365
Platform
  1. MacOS
I've added my code below, although I'm nowhere close to my desired task.

I'm trying to sort ws1 in a way that Column A (which contains either YES, NO, MDR, or DPL) will be in order of all of the YES & MDR together, followed by all of the NO, with DPL sifted throughout as required. From here, the YES and MDR should be in alphabetical order by name in column B. And the NO should be in alphabetical order by name after the YES and MDR group. To confuse it even more, there are some duplicate names with DPL in Column A. These should be fit in with the matching name, with no care for whether the matching name is YES, NO, or MDR. Lastly, for any name with DPLs attached to it, Column E should be used to sort them in chronological order, oldest date on top. I know this is quite confusing to read. Here's an example that I hope explains it better. btw, the data set starts in Row 7.

[TABLE="width: 500"]
<tbody>[TR]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]E[/TD]
[/TR]
[TR]
[TD]YES[/TD]
[TD]Cole, Mike[/TD]
[TD]2 Feb 2019[/TD]
[/TR]
[TR]
[TD]DPL[/TD]
[TD]Cole, Mike[/TD]
[TD]16 Feb 2019[/TD]
[/TR]
[TR]
[TD]MDR[/TD]
[TD]Hotel, Jim[/TD]
[TD]4 Feb 2019[/TD]
[/TR]
[TR]
[TD]DPL[/TD]
[TD]Hotel, Jim[/TD]
[TD]9 Feb 2019[/TD]
[/TR]
[TR]
[TD]YES[/TD]
[TD]Jones, Sam[/TD]
[TD]2 Feb 2019[/TD]
[/TR]
[TR]
[TD]YES[/TD]
[TD]Lupo, Mike[/TD]
[TD]2 Feb 2019[/TD]
[/TR]
[TR]
[TD]MDR[/TD]
[TD]Patches, Tim[/TD]
[TD]2 Feb 2019[/TD]
[/TR]
[TR]
[TD]YES[/TD]
[TD]Smith, John[/TD]
[TD]2 Feb 2019[/TD]
[/TR]
[TR]
[TD]DPL[/TD]
[TD]Smith, John[/TD]
[TD]10 Feb 2019[/TD]
[/TR]
[TR]
[TD]DPL[/TD]
[TD]Smith, John[/TD]
[TD]28 Feb 2019[/TD]
[/TR]
[TR]
[TD]NO[/TD]
[TD]Adams, Matt[/TD]
[TD]2 Feb 2019[/TD]
[/TR]
[TR]
[TD]NO[/TD]
[TD]Baker, Kyle[/TD]
[TD]2 Feb 2019[/TD]
[/TR]
[TR]
[TD]DPL[/TD]
[TD]Baker, Kyle[/TD]
[TD]19 Feb 2019[/TD]
[/TR]
[TR]
[TD]DPL[/TD]
[TD]Baker, Kyle[/TD]
[TD]22 Feb 2019[/TD]
[/TR]
[TR]
[TD]NO[/TD]
[TD]Phelps, Tom[/TD]
[TD]4 Feb 2019[/TD]
[/TR]
[TR]
[TD]NO[/TD]
[TD]Roman, Tyler[/TD]
[TD]2 Feb 2019[/TD]
[/TR]
</tbody>[/TABLE]


Code:
Sub sortCol()

Dim ws1     As Worksheet: Set ws1 = ThisWorkbook.Sheets("Main Data")
Dim vSortList     As Variant
Dim LrC     As Long
Dim x     As Integer

ws1.Sort.SortFields.Clear

LrC = ws1.Range("A" & Rows.Count).End(xlUp).Row
vSortList = Array("YES", "MDR", "NO", "DPL")

On Error Resume Next

Application.AddCustomList ListArray:=vSortList
ws1.Range("A7:Y" & LrC).Sort Key1:=[E7], Order1:=xlAscending     'Date
ws1.Range("A7:Y" & LrC).Sort Key1:=[A7], ordercustos:=Application.CustomListCount + 1
ws1.Range("A7:Y" & LrC).Sort Key1:=[B7], Order1:=xlAscending     'Name

x = Application.GetCustomListNum(ListArray:=vSortList)
Application.DeleteCustomList x

ws1.Sort.SortFields.Clear

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Giving this a bump since I'm in a weird time zone. The best answers always show up after I go to bed!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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