Sorting with VBA

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
289
Office Version
  1. 2016
Platform
  1. Windows
Trying to sort this using VBA to have airline alphabetically then by time for each date. I've made it this far but can't figure the rest... I need to keep the spaces between the dates. Any help would be appreceiated. Thanks!


PIMS Flight Converter.xlsm
ABCDE
1Airport Total by Departure Time-Flight#-Airline
2
3DateAirlineTimePAXPCPAX
47/31/2023AA51605125030
57/31/2023SW19135405030
67/31/2023UA35406005030
77/31/2023UA43726005030
87/31/2023AA55896005030
97/31/2023AA61386065030
107/31/2023SW19156305030
117/31/2023SP20447005030
127/31/2023SW5369055030
137/31/2023SW345912105030
147/31/2023AA515912365030
157/31/2023UA356312495030
167/31/2023AA604013065030
177/31/2023AA545713505030
187/31/2023UA459114035030
197/31/2023SW264814105030
207/31/2023SW307415055030
217/31/2023AA591715535030
227/31/2023SW237017105030
237/31/2023AA504017165030
247/31/2023AA502117275030
257/31/2023SW18119155030
267/31/2023AA538019155030
27
287/30/2023AA51605115030
297/30/2023SW19135505030
307/30/2023UA35406005030
317/30/2023UA43726005030
327/30/2023AA55896005030
337/30/2023AA61386065030
347/30/2023SP20447005030
357/30/2023SW22297055030
367/30/2023DL60310005030
377/30/2023SW345910255030
387/30/2023SP115911055030
397/30/2023AA515912365030
407/30/2023UA356312495030
417/30/2023AA604013065030
427/30/2023SW95213205030
437/30/2023AA545713505030
447/30/2023UA459114035030
457/30/2023AA591715535030
467/30/2023AA504017165030
477/30/2023AA502117175030
487/30/2023SW362917255030
497/30/2023AA538019155030
50
517/29/2023SW23685005030
527/29/2023AA51605125030
537/29/2023SW19136005030
547/29/2023UA35406005030
557/29/2023UA43726005030
567/29/2023AA55896005030
577/29/2023AA61386065030
587/29/2023SP20447005030
597/29/2023SW167111505030
607/29/2023UA356312155030
617/29/2023AA336712365030
627/29/2023SW345912555030
637/29/2023AA604013065030
647/29/2023SW95213505030
657/29/2023AA497414035030
667/29/2023AA591716105030
677/29/2023SW237016255030
687/29/2023AA504017165030
697/29/2023SW330017355030
707/29/2023SW133818505030
717/29/2023AA538019165030
72
73000
74000
75000
76000
77000
Converted Sheet
 
As far as the Airlines already being sorted by time by default, that is an unknow at this time. Maybe a snippet somewhere just in case the default is not always true?.....
Ok, I'll amend the code.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this, I added a part to sort by time.

VBA Code:
Sub RandyD123_5()
Dim c As Range
Application.ScreenUpdating = False

'generate sheet output "Result", overwright old sheet
On Error Resume Next
    Sheets("Result").Delete
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Result"
    Sheets("Raw Data").Cells.Copy Range("A1")
On Error GoTo 0

With Sheets("Sheet2")  'replace Airline with 2 letters
    For Each c In .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
        Range("G:G").Replace What:=c.Value, Replacement:=c.Offset(, 1), LookAt:=xlWhole, SearchOrder:=xlByRows, _
        MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Next
End With

n = Range("A" & Rows.Count).End(xlUp).Row
h = 4 'data start at row

'sort by 2 first letter
For i = n To h Step -1
    j = WorksheetFunction.CountIf(Range("A" & n & ":A" & h), Cells(i, "A")) 'case insensitive

'        Debug.Print Cells(i - j + 1, "A").Resize(j, 11).Address
        Cells(i - j + 1, "A").Resize(j, 11).sort Key1:=Columns("G"), Order1:=xlAscending, Header:=xlNo  'sort by 2 first letter
        If i - j + 1 <> h Then
            Rows(i - j + 1).Insert  'insert row between different dates
        End If
        i = i - j + 1
Next

'TIME to number format
For Each c In Range("E4", Cells(Rows.Count, "E").End(xlUp))
    c = Replace(Left(c.Text, 6), ":", "")
Next

'sort by TIME
Set f = Range("A4", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)

    For Each c In f.Areas
            n = Split(c.Resize(, 2).Address, "$")(4) 'last row of areas
            For i = c.Row To n
            j = WorksheetFunction.CountIf(Range("G" & i & ":G" & n), Cells(i, "G")) 'case insensitive
                With Cells(i, "A").Resize(j, 11)
                        .sort Key1:=.Columns("E"), Order1:=xlAscending, Header:=xlNo  'sort by time
                End With
                i = i + j - 1
            Next
    Next

'combine code & flight number
With Range("B4", Range("B" & Rows.Count).End(xlUp))
   .Value = Evaluate(.Columns(6).Address & "&" & .Columns(5).Address)
End With

Range("C:D,F:H,J:J").Delete
Range("A3:E3").Value = Array("Date", "Airline", "Time", "PAX", "PCPAX")
Application.ScreenUpdating = True

End Sub
 
Upvote 0
This one is a bit shorter:
VBA Code:
Sub RandyD123_6()
Dim c As Range
Application.ScreenUpdating = False

'generate sheet output "Result", overwright old sheet
On Error Resume Next
    Sheets("Result").Delete
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Result"
    Sheets("Raw Data").Cells.Copy Range("A1")
On Error GoTo 0

With Sheets("Sheet2")  'replace Airline with 2 letters
    For Each c In .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
        Range("G:G").Replace What:=c.Value, Replacement:=c.Offset(, 1), LookAt:=xlWhole, SearchOrder:=xlByRows, _
        MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Next
End With

'TIME to number format
For Each c In Range("E4", Cells(Rows.Count, "E").End(xlUp))
    c = Replace(Left(c.Text, 6), ":", "")
Next

n = Range("A" & Rows.Count).End(xlUp).Row
h = 4 'data start at row

'sort by 2 first letter
For i = n To h Step -1
    j = WorksheetFunction.CountIf(Range("A" & n & ":A" & h), Cells(i, "A")) 'case insensitive
        'sort by code then by time
        Cells(i - j + 1, "A").Resize(j, 11).sort Key1:=Columns("G"), Order1:=xlAscending, Key2:=Columns("E"), Order1:=xlAscending, Header:=xlNo
        If i - j + 1 <> h Then
            Rows(i - j + 1).Insert  'insert row between different dates
        End If
        i = i - j + 1
Next

'combine code & flight number
With Range("B4", Range("B" & Rows.Count).End(xlUp))
   .Value = Evaluate(.Columns(6).Address & "&" & .Columns(5).Address)
End With

Range("C:D,F:H,J:J").Delete
Range("A3:E3").Value = Array("Date", "Airline", "Time", "PAX", "PCPAX")
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Thanks to you and bebo021999 for taking the time to help me out. I can't express my gratitude enough. It's funny how 2 people who know how to code can have slightly different vba and yet it produces the same end result. In the end I went with you because of the "sheet2" idea. That is exactly what I needed and got without asking. Regional airlines are funny because the same regional can operate for two different major carriers. A case in point is Republic Airlines, they operate for AA and UA out of the same airport (MHT). I can tell by the flight number who the major is and it's only two flights that are affected for this whole conversion thing. Again, thanks for all the help.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,335
Messages
6,124,327
Members
449,155
Latest member
ravioli44

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