Formatting using VBA

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
296
Office Version
  1. 2016
Platform
  1. Windows
Looking to get a little help with some formatting via VBA. I have 2 files that I use to make a conversion....so to speak. My first file is a "Raw Data" file. This file is what I copy into my second file "Flight Converter". I run my macro in the Converter file and it simply formats my data into a format that I can use for something else. I just need to continue with a little more VBA. If anyone is willing to download my 2 files you will be able to see how far I've got. I show on the "desired output" tab of the conversion file what I'm looking to do. You will notice that on the desired output the flights are listed alphabetically by time, this is my last step. You might notice that on the converted file that my times continue beyond what I really need and that's because I didn't know how to include for expansion in the vba code, meaning that the raw data file may have more or less rows on some days than other days. The columns will always be the same

Link to Raw Data
Link to Converter

So far I have this:

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

Thank you in advance for any help with this.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

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