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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
See if it does what you want:
VBA Code:
Sub RandyD123()

Dim c As Range, f As Range

Application.ScreenUpdating = False
'"A4" means start at A4
Set f = Range("A4", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)

For Each c In f.Areas
'Debug.Print c.Address

    With c.Resize(, 5) 'expand to col A:E
        .Sort Key1:=.Columns(2), Order1:=xlAscending, Header:=xlNo
    End With
Next
Application.ScreenUpdating = True

End Sub

but you probably doesn't like the result such as:
SW3459
SW536
this is because your data is alphanumeric, therefore it is sorted as text not number.
However, we can amend the code to also sort the number properly, but we need a temporary helper column, say col G.

Book1
ABCDE
1Airport Total by Departure Time-Flight#-Airline
2
3DateAirlineTimePAXPCPAX
47/31/2023AA502117275030
57/31/2023AA504017165030
67/31/2023AA515912365030
77/31/2023AA51605125030
87/31/2023AA538019155030
97/31/2023AA545713505030
107/31/2023AA55896005030
117/31/2023AA591715535030
127/31/2023AA604013065030
137/31/2023AA61386065030
147/31/2023SP20447005030
157/31/2023SW18119155030
167/31/2023SW19135405030
177/31/2023SW19156305030
187/31/2023SW237017105030
197/31/2023SW264814105030
207/31/2023SW307415055030
217/31/2023SW345912105030
227/31/2023SW5369055030
237/31/2023UA35406005030
247/31/2023UA356312495030
257/31/2023UA43726005030
267/31/2023UA459114035030
27
287/30/2023AA502117175030
297/30/2023AA504017165030
307/30/2023AA515912365030
317/30/2023AA51605115030
327/30/2023AA538019155030
337/30/2023AA545713505030
347/30/2023AA55896005030
357/30/2023AA591715535030
367/30/2023AA604013065030
377/30/2023AA61386065030
387/30/2023DL60310005030
397/30/2023SP115911055030
407/30/2023SP20447005030
417/30/2023SW19135505030
427/30/2023SW22297055030
437/30/2023SW345910255030
447/30/2023SW362917255030
457/30/2023SW95213205030
467/30/2023UA35406005030
477/30/2023UA356312495030
487/30/2023UA43726005030
497/30/2023UA459114035030
50
517/29/2023AA336712365030
527/29/2023AA497414035030
537/29/2023AA504017165030
547/29/2023AA51605125030
557/29/2023AA538019165030
567/29/2023AA55896005030
577/29/2023AA591716105030
587/29/2023AA604013065030
597/29/2023AA61386065030
607/29/2023SP20447005030
617/29/2023SW133818505030
627/29/2023SW167111505030
637/29/2023SW19136005030
647/29/2023SW23685005030
657/29/2023SW237016255030
667/29/2023SW330017355030
677/29/2023SW345912555030
687/29/2023SW95213505030
697/29/2023UA35406005030
707/29/2023UA356312155030
717/29/2023UA43726005030
Sheet1
 
Upvote 0
As long as the end result has the flights alphabetically meaning all AA then DL then SP then SW then UA by time for each date section. Then end result should look like this....


PIMS Flight Converter.xlsm
ABCDE
1Airport Total by Departure Time-Flight#-Airline
2
3DateAirlineTimePAXPCPAX
47/31/2023AA51605125030
57/31/2023AA55896005030
67/31/2023AA61386065030
77/31/2023AA515912365030
87/31/2023AA604013065030
97/31/2023AA545713505030
107/31/2023AA591715535030
117/31/2023AA504017165030
127/31/2023AA502117275030
137/31/2023AA538019155030
147/31/2023SP20447005030
157/31/2023SW19135405030
167/31/2023SW19156305030
177/31/2023SW5369055030
187/31/2023SW345912105030
197/31/2023SW264814105030
207/31/2023SW307415055030
217/31/2023SW237017105030
227/31/2023SW18119155030
237/31/2023UA35406005030
247/31/2023UA43726005030
257/31/2023UA356312495030
267/31/2023UA459114035030
27
287/30/2023AA51605115030
297/30/2023AA55896005030
307/30/2023AA61386065030
317/30/2023AA515912365030
327/30/2023AA604013065030
337/30/2023AA545713505030
347/30/2023AA591715535030
357/30/2023AA504017165030
367/30/2023AA502117175030
377/30/2023AA538019155030
387/30/2023DL60310005030
397/30/2023SP20447005030
407/30/2023SP115911055030
417/30/2023SW19135505030
427/30/2023SW22297055030
437/30/2023SW345910255030
447/30/2023SW362917255030
457/30/2023SW95213205030
467/30/2023UA35406005030
477/30/2023UA43726005030
487/30/2023UA356312495030
497/30/2023UA459114035030
50
517/29/2023AA51605125030
527/29/2023AA55896005030
537/29/2023AA61386065030
547/29/2023AA336712365030
557/29/2023AA604013065030
567/29/2023AA497414035030
577/29/2023AA591716105030
587/29/2023AA504017165030
597/29/2023AA538019165030
607/29/2023SP20447005030
617/29/2023SW23685005030
627/29/2023SW19136005030
637/29/2023SW167111505030
647/29/2023SW345912555030
657/29/2023SW95213505030
667/29/2023SW237016255030
677/29/2023SW330017355030
687/29/2023SW133818505030
697/29/2023UA35406005030
707/29/2023UA43726005030
717/29/2023UA356312155030
72
73000
Desired Outpu
 
Upvote 0
What code do you have? Have you tried recording a macro?
VBA Code:
Sub Convert()
'
' Convert Macro
'

'
    Columns("A:K").Select
    Selection.ColumnWidth = 19.57
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("B:B,C:C,D:D,H:H,J:J").Select
    Range("J1").Activate
    Selection.Delete shift:=xlToLeft
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 1
    Columns("C:C").Select
    Selection.Cut
    Columns("E:E").Select
    Selection.Insert shift:=xlToRight
    Columns("B:B").Select
    Selection.Cut
    Columns("E:E").Select
    Selection.Insert shift:=xlToRight
    Range("B4").Select
    Cells.Replace What:="Comair Inc.", Replacement:="AA", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Cells.Replace What:="Southwest Airlines Co.", Replacement:="SW", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Cells.Replace What:="Republic Airline Inc.", Replacement:="UA", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Cells.Replace What:="Spirit Air Lines", Replacement:="SP", LookAt:=xlPart _
        , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Columns("B:B").ColumnWidth = 25.14
    Cells.Replace What:="Capital Cargo International", Replacement:="AA", _
        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
        False, ReplaceFormat:=False
    Cells.Replace What:="Envoy Inc.", Replacement:="AA", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Cells.Replace What:="Skywest Airlines Inc.", Replacement:="AA", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Cells.Replace What:="Avelo Airlines", Replacement:="DL", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Columns("D:D").Select
    Selection.Insert shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]&RC[-1]"
    Range("D4").Select
    Selection.AutoFill Destination:=Range("D4:D75"), Type:=xlFillDefault
    Range("D4:D75").Select
    ActiveWindow.SmallScroll Down:=-48
    Range("D4:D75").Select
    Selection.Copy
    Range("D4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("B:C").Select
    Application.CutCopyMode = False
    Selection.Delete shift:=xlToLeft
    Columns("D:D").Select
    Selection.Insert shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "=TEXT(RC[-1], ""hmm"")"
    Range("D4").Select
    Selection.AutoFill Destination:=Range("D4:D75"), Type:=xlFillDefault
    Range("D4:D75").Select
    ActiveWindow.SmallScroll Down:=-33
    Range("D4:D75").Select
    Selection.Copy
    Range("D4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("C:C").Select
    Application.CutCopyMode = False
    Selection.Delete shift:=xlToLeft
    Range("B3").Select
    ActiveCell.FormulaR1C1 = "Airline"
    Range("C3").Select
    ActiveCell.FormulaR1C1 = "Time"
    Range("B4").Select
    
'Code below will insert spaces between dates

Dim iRow As Integer, iCol As Integer
Dim oRng As Range

Set oRng = Range("A4")

iRow = oRng.Row
iCol = oRng.Column

Do
'
If Cells(iRow + 1, iCol) <> Cells(iRow, iCol) Then
    Cells(iRow + 1, iCol).EntireRow.Insert shift:=xlDown
    iRow = iRow + 2
Else
    iRow = iRow + 1
End If
'
Loop While Not Cells(iRow, iCol).Text = ""


'Code below will change the time column from text to a number format

With Range("c4:c" & Cells(Rows.Count, 1).End(xlUp).Row)
  .NumberFormat = "General"
  .Value = .Value
 End With
    
End Sub
 
Upvote 0
As long as the end result has the flights alphabetically meaning all AA then DL then SP then SW then UA by time for each date section. Then end result should look like this....
Try this one:
VBA Code:
Sub RandyD123_2()

Dim c As Range, f As Range
Dim i As Long, j As Long
Dim va
Application.ScreenUpdating = False
'"A4" means start at A4
Set f = Range("A4", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)

For Each c In f.Areas
    With c.Resize(, 5) 'expand to col A:E
        .Sort Key1:=.Columns(2), Order1:=xlAscending, Header:=xlNo
    End With
Next

n = Range("B" & Rows.Count).End(xlUp).Row
va = Range("B1", Cells(Rows.Count, "B").End(xlUp))
va(3, 1) = "---"
For i = 4 To UBound(va, 1)
     j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While Left(va(i, 1), 2) = Left(va(i - 1, 1), 2)
    i = i - 1
    
    If i > j Then
        Range(Cells(j, "A"), Cells(i, "E")).Sort Key1:=Columns(3), Order1:=xlAscending, Header:=xlNo
    End If
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this one:
VBA Code:
Sub RandyD123_2()

Dim c As Range, f As Range
Dim i As Long, j As Long
Dim va
Application.ScreenUpdating = False
'"A4" means start at A4
Set f = Range("A4", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)

For Each c In f.Areas
    With c.Resize(, 5) 'expand to col A:E
        .Sort Key1:=.Columns(2), Order1:=xlAscending, Header:=xlNo
    End With
Next

n = Range("B" & Rows.Count).End(xlUp).Row
va = Range("B1", Cells(Rows.Count, "B").End(xlUp))
va(3, 1) = "---"
For i = 4 To UBound(va, 1)
     j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While Left(va(i, 1), 2) = Left(va(i - 1, 1), 2)
    i = i - 1
   
    If i > j Then
        Range(Cells(j, "A"), Cells(i, "E")).Sort Key1:=Columns(3), Order1:=xlAscending, Header:=xlNo
    End If
Next
Application.ScreenUpdating = True
End Sub
That seems to work perfectly! Thank You!
 
Upvote 0
That seems to work perfectly! Thank You!
You're welcome, glad to help & thanks for the feedback.:)
Note: I am assuming that the airline codes always consist of 2 letters followed by numbers.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
Note: I am assuming that the airline codes always consist of 2 letters followed by numbers.
Yes that is correct. You can see in my vb code that I had to take the regional airline name and convert them to a 2 letter major carrier followed by flight number. The only issue I may have is that the time format was a real bear for me as I needed a number format and because the number of rows for each date could change on a daily basis, I had to extend the time format column, that's why the "000" at the end of the file. Wasn't sure how to resolve that.... :)
 
Upvote 0
The only issue I may have is that the time format was a real bear for me as I needed a number format
'Code below will change the time column from text to a number format

With Range("c4:c" & Cells(Rows.Count, 1).End(xlUp).Row)
.NumberFormat = "General"
.Value = .Value
End With

So, originally the time is in text format instead of time format?
How did the data load into the sheet? from a text file?
Can you post data before you run the Sub Convert?
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,295
Members
449,149
Latest member
mwdbActuary

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