excelvbasortingfilter

Filters and then sorting


I have a code that works, however I am working on cleaning up my codes I use. This code is stored in ThisWorkbook and run under Workbook_BeforeClose(Cancel As Boolean). Is there a cleaner way of doing it, rather shortening it. Since it is working with filter and sorting there is a 3 blank column in between each day set. Each day contains a different amount of times.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

' Sort Macro
 '
Application.ScreenUpdating = False

Worksheets("RoadMap").Activate
    Range("C3:D151").Select '''Selects column
    Application.DeleteCustomList ListNum:=5
    Application.AddCustomList ListArray:=Array("TRN", "PIT-G", "PIT-D", "PIT-S", "F-230A", "F-330A", "F-430A", "F-830A", "F-930A", "F-1030A", "F-1130A", "F-1230P", "F-130P", "F-230P", "F-430P", "F-530P", "F-630P", "F-730P", "F-830P", "F-930P", "4AM", "5AM", "T-6AM", "8AM", "9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM", "4PM", "5PM", "T-6PM", "6PM", "7PM", "8PM", "9PM", "10PM", "11PM") 'Column contains these starts to be in this order
       '''''Data will be added daily and sorted into proper order
    ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Add Key:=Range("C3:C151"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= "TRN,PIT-G,PIT-D,PIT-S,F-230A,F-330A,F-430A,F-830A,F-930A,F-1030A,F-1130A,F-1230P,F-130P,F-230P,F-430P,F-530P,F-630P,F-730P,F-830P,F-930P,4AM,5AM,T-6AM,8AM,9AM,10AM,11AM,12PM,1PM,2PM,3PM,4PM,5PM,T-6PM,6PM,7PM,8PM,9PM,10PM,11PM", DataOption:=xlSortNormal 

With ActiveWorkbook.Worksheets("RoadMap").Sort
    .SetRange Range("C2:D151")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
 '''''Repeats for each column that has the data for days of the week M-S
    Range("H3:I151").Select 'repeats previous for the next range of columns with data
    Application.DeleteCustomList ListNum:=5
    Application.AddCustomList ListArray:=Array("TRN", "PIT-G", "PIT-D", "PIT-S", _
    "F-230A", "F-330A", "F-430A", "F-830A", "F-930A", "F-1030A", "F-1130A", "F-1230P", "F-130P", "F-230P", "F-430P", "F-530P", "F-630P", "F-730P", "F-830P", "F-930P", "4AM", "5AM", _
    "T-6AM", "8AM", "9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM", "4PM", "5PM", "T-6PM", "6PM", "7PM", "8PM", "9PM", "10PM", "11PM")
  'sorting order
    ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Add Key:=Range("H4:H151"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= "TRN,PIT-G,PIT-D,PIT-S,F-230A,F-330A,F-430A,F-830A,F-930A,F-1030A,F-1130A,F-1230P,F-130P,F-230P,F-430P,F-530P,F-630P,F-730P,F-830P,F-930P,4AM,5AM,T-6AM,8AM,9AM,10AM,11AM,12PM,1PM,2PM,3PM,4PM,5PM,T-6PM,6PM,7PM,8PM,9PM,10PM,11PM", DataOption:=xlSortNormal
 'sorting
With ActiveWorkbook.Worksheets("RoadMap").Sort
    .SetRange Range("H3:I151")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
'next column
Range("M3:N151").Select
    Application.DeleteCustomList ListNum:=5
    Application.AddCustomList ListArray:=Array("TRN", "PIT-G", "PIT-D", "PIT-S", "F-230A", "F-330A", "F-430A", "F-830A", "F-930A", "F-1030A", "F-1130A", "F-1230P", "F-130P", "F-230P", "F-430P", "F-530P", "F-630P", "F-730P", "F-830P", "F-930P", "4AM", "5AM", "T-6AM", "8AM", "9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM", "4PM", "5PM", "T-6PM", "6PM", "7PM", "8PM", "9PM", "10PM", "11PM")
'sorting order
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Clear 
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Add Key:=Range("M4:M151"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= "TRN,PIT-G,PIT-D,PIT-S,F-230A,F-330A,F-430A,F-830A,F-930A,F-1030A,F-1130A,F-1230P,F-130P,F-230P,F-430P,F-530P,F-630P,F-730P,F-830P,F-930P,4AM,5AM,T-6AM,8AM,9AM,10AM,11AM,12PM,1PM,2PM,3PM,4PM,5PM,T-6PM,6PM,7PM,8PM,9PM,10PM,11PM", DataOption:=xlSortNormal
'sorting
With ActiveWorkbook.Worksheets("RoadMap").Sort
    .SetRange Range("M3:N151")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
'next column
Range("R3:S151").Select
    Application.DeleteCustomList ListNum:=5
    Application.AddCustomList ListArray:=Array("TRN", "PIT-G", "PIT-D", "PIT-S", "F-230A", "F-330A", "F-430A", "F-830A", "F-930A", "F-1030A", "F-1130A", "F-1230P", "F-130P", "F-230P", "F-430P", "F-530P", "F-630P", "F-730P", "F-830P", "F-930P", "4AM", "5AM",  "T-6AM", "8AM", "9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM", "4PM", "5PM", "T-6PM", "6PM", "7PM", "8PM", "9PM", "10PM", "11PM")
'sorting order
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Add Key:=Range("R4:R151"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= "TRN,PIT-G,PIT-D,PIT-S,F-230A,F-330A,F-430A,F-830A,F-930A,F-1030A,F-1130A,F-1230P,F-130P,F-230P,F-430P,F-530P,F-630P,F-730P,F-830P,F-930P,4AM,5AM,T-6AM,8AM,9AM,10AM,11AM,12PM,1PM,2PM,3PM,4PM,5PM,T-6PM,6PM,7PM,8PM,9PM,10PM,11PM", DataOption:=xlSortNormal
'sorting
With ActiveWorkbook.Worksheets("RoadMap").Sort
    .SetRange Range("R3:S151")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
'next column
Range("W3:X151").Select
    Application.DeleteCustomList ListNum:=5
    Application.AddCustomList ListArray:=Array("TRN", "PIT-G", "PIT-D", "PIT-S", "F-230A", "F-330A", "F-430A", "F-830A", "F-930A", "F-1030A", "F-1130A", "F-1230P", "F-130P", "F-230P", "F-430P", "F-530P", "F-630P", "F-730P", "F-830P", "F-930P", "4AM", "5AM", "T-6AM", "8AM", "9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM", "4PM", "5PM", "T-6PM", "6PM", "7PM", "8PM", "9PM", "10PM", "11PM")
'sorting order
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Add Key:=Range("W4:W151"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= "TRN,PIT-G,PIT-D,PIT-S,F-230A,F-330A,F-430A,F-830A,F-930A,F-1030A,F-1130A,F-1230P,F-130P,F-230P,F-430P,F-530P,F-630P,F-730P,F-830P,F-930P,4AM,5AM,T-6AM,8AM,9AM,10AM,11AM,12PM,1PM,2PM,3PM,4PM,5PM,T-6PM,6PM,7PM,8PM,9PM,10PM,11PM", DataOption:=xlSortNormal
'sorting
With ActiveWorkbook.Worksheets("RoadMap").Sort
    .SetRange Range("W3:X151")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
 '''next column
Range("AB3:AC151").Select
    Application.DeleteCustomList ListNum:=5
    Application.AddCustomList ListArray:=Array("TRN", "PIT-G", "PIT-D", "PIT-S", "F-230A", "F-330A", "F-430A", "F-830A", "F-930A", "F-1030A", "F-1130A", "F-1230P", "F-130P", "F-230P", "F-430P", "F-530P", "F-630P", "F-730P", "F-830P", "F-930P", "4AM", "5AM", "T-6AM", "8AM", "9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM", "4PM", "5PM", "T-6PM", "6PM", "7PM", "8PM", "9PM", "10PM", "11PM")
'sorting order
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Add Key:=Range("AB4:AB151"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= "TRN,PIT-G,PIT-D,PIT-S,F-230A,F-330A,F-430A,F-830A,F-930A,F-1030A,F-1130A,F-1230P,F-130P,F-230P,F-430P,F-530P,F-630P,F-730P,F-830P,F-930P,4AM,5AM,T-6AM,8AM,9AM,10AM,11AM,12PM,1PM,2PM,3PM,4PM,5PM,T-6PM,6PM,7PM,8PM,9PM,10PM,11PM", DataOption:=xlSortNormal
'sorting
With ActiveWorkbook.Worksheets("RoadMap").Sort
    .SetRange Range("AB3:AC151")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
'next column
Range("AG3:AH151").Select
    Application.DeleteCustomList ListNum:=5
    Application.AddCustomList ListArray:=Array("TRN", "PIT-G", "PIT-D", "PIT-S", "F-230A", "F-330A", "F-430A", "F-830A", "F-930A", "F-1030A", "F-1130A", "F-1230P", "F-130P", "F-230P", "F-430P", "F-530P", "F-630P", "F-730P", "F-830P", "F-930P", "4AM", "5AM", "T-6AM", "8AM", "9AM", "10AM", "11AM", "12PM", "1PM", "2PM", "3PM", "4PM", "5PM", "T-6PM", "6PM", "7PM", "8PM", "9PM", "10PM", "11PM")
''' column sort
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RoadMap").Sort.SortFields.Add Key:=Range("AG4:AG151"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= "TRN,PIT-G,PIT-D,PIT-S,F-230A,F-330A,F-430A,F-830A,F-930A,F-1030A,F-1130A,F-1230P,F-130P,F-230P,F-430P,F-530P,F-630P,F-730P,F-830P,F-930P,4AM,5AM,T-6AM,8AM,9AM,10AM,11AM,12PM,1PM,2PM,3PM,4PM,5PM,T-6PM,6PM,7PM,8PM,9PM,10PM,11PM", DataOption:=xlSortNormal

With ActiveWorkbook.Worksheets("RoadMap").Sort
    .SetRange Range("AG3:AH151")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Range("A2").Select

End Sub

Solution

  • Shorten Code Using a Loop: Custom Sort Data

    Before

    enter image description here

    After

    enter image description here

    Sub SortData()
    
        Const SHEET_NAME As String = "RoadMap"
        Const FIRST_RANGE_ADDRESS As String = "C3:D22"
        Const SORT_LIST As String = "" _
            & "TRN,PIT-G,PIT-D,PIT-S,F-230A," _
            & "F-330A,F-430A,F-830A,F-930A,F-1030A," _
            & "F-1130A,F-1230P,F-130P,F-230P,F-430P," _
            & "F-530P,F-630P,F-730P,F-830P,F-930P," _
            & "4AM,5AM,T-6AM,8AM,9AM," _
            & "10AM,11AM,12PM,1PM,2PM," _
            & "3PM,4PM,5PM,T-6PM,6PM," _
            & "7PM,8PM,9PM,10PM,11PM"
        Const DAYS_COUNT As Long = 7
        Const COLUMN_OFFSET As Long = 5
        
        Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
        Dim ws As Worksheet: Set ws = wb.Sheets(SHEET_NAME)
        Dim rg As Range: Set rg = ws.Range(FIRST_RANGE_ADDRESS)
        
        Application.ScreenUpdating = False
        
        Dim srg As Range, n As Long
            
        For n = 1 To DAYS_COUNT
            Set srg = rg.Offset(, (n - 1) * COLUMN_OFFSET)
            Debug.Print n, srg.Address
            With ws.Sort
                With .SortFields
                    .Clear
                    .Add _
                        Key:=srg.Columns(1), _
                        SortOn:=xlSortOnValues, _
                        Order:=xlAscending, _
                        CustomOrder:=SORT_LIST, _
                        DataOption:=xlSortNormal
                End With
                .SetRange srg
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .Apply
            End With
        Next n
        
        Application.ScreenUpdating = True
    
    End Sub