Insert missing date if one exists

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In the attached worksheet, I would like to identify a missing date, insert the date in Column D. Something like 24 Oct 2020 0000 and a statement "NO DEPARTURES" in Column A. I have a VBA that sequences the days. If there is a missing day in the departures, the following days schedule will get posted to the previous day's schedule. I provided example what the end product I hope will look like. The Roll Call will only display the time once complete.

MISSING DATE.JPG
Outbound 72 Hr Schedule Mr Excel.xlsm
ABCD
1(LAX ) LOS ANGELES, CA802B77723 Oct 2020 1140
2(SFO ) SAN FRANCISCO, CAA716B77723 Oct 2020 1156
3(LEX ) LEXINGTON, KY08TZB73723 Oct 2020 1210
4(MIA ) MIAMI INTL, FL805B73723 Oct 2020 1340
5(SEA ) SEATTLE TACOMA INTL, WARC99B73723 Oct 2020 1440
6(OCF ) OCALA, FLRC99B73723 Oct 2020 1510
7(OCF ) OCALA, FLRC99B73723 Oct 2020 1540
8(TOL ) TOLEDO, OH8441B77723 Oct 2020 1735
9(TOL ) TOLEDO, OH08TAB77723 Oct 2020 2339
10(ACV ) ARCATA/MCKINLEYVILLE, CA6241B77725 Oct 2020 1905
11(SFO ) SAN FRANCISCO, CA6251B77726 Oct 2020 1240
72 Hr
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
try this on a copy of your file.

VBA Code:
Sub Do_it()
Dim d1 As Date, d2 As Date
For r = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1

d1 = CDate(Split(Cells(r, "D"), " ")(1) & ", " & Split(Cells(r, "D"), " ")(0) & " " & Split(Cells(r, "D"), " ")(2))
d2 = CDate(Split(Cells(r - 1, "D"), " ")(1) & ", " & Split(Cells(r - 1, "D"), " ")(0) & " " & Split(Cells(r - 1, "D"), " ")(2))


If d1 - d2 >= 2 Then

Rows(r).Insert shift:=xlDown

Cells(r, "D") = Format(d1 - 1, "dd mmm yyyy 0000")
Cells(r, "A") = "NO DEPARTURES"
End If
Next


End Sub
 
Upvote 0
try this on a copy of your file.

VBA Code:
Sub Do_it()
Dim d1 As Date, d2 As Date
For r = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1

d1 = CDate(Split(Cells(r, "D"), " ")(1) & ", " & Split(Cells(r, "D"), " ")(0) & " " & Split(Cells(r, "D"), " ")(2))
d2 = CDate(Split(Cells(r - 1, "D"), " ")(1) & ", " & Split(Cells(r - 1, "D"), " ")(0) & " " & Split(Cells(r - 1, "D"), " ")(2))


If d1 - d2 >= 2 Then

Rows(r).Insert shift:=xlDown

Cells(r, "D") = Format(d1 - 1, "dd mmm yyyy 0000")
Cells(r, "A") = "NO DEPARTURES"
End If
Next


End Sub
That is spot on, that is one of about 20 VBAs I have for this program. It ran nearly perfect and it will be good for posting no flight departures a few days out. I did have to tweak just two things: I had to put N/A in a couple of the columns because another VBA wouldn’t work properly, and I had to change the time since I have to subtract 7 hours from the time to get a local time. This was on me, I didn’t account for that when I sent out the original post.

However, I ran it to track flights for the next 7 days and there are three days that ran consecutively with no flights, and it inserts just one day, if I hit F5 again it will insert the next one and so on. Naturally, this is not the most efficient since I’m going to have a micro button on my ribbon. The other issue which I wasn’t quite sure of is it will not insert a date for the current date. A few VBAs after the one you created I have a VBA “Boarders” and that is what your see on the image I sent on my original post. Is there any way we can get the Boarders VBA to recognize there are no flights on the current date, if so then I’ll be looking for the same sort of “NO DEPARTURES” in Column A, and N/A for Columns B and C? The last step after that to this enormous undertaking is isolate time in Column D without a colon and to have zeros in front of the time like 0245.

I went and posted the entirety of my VBAs to provide context. You will see the one you sent. With the exert provided yesterday that was achieved after the VBA Sub concatMyData().

Thank you,

VBA Code:
Sub delCol()
        Dim sourceSheet As Worksheet
        Set sourceSheet = Sheet1
     sourceSheet.Range("A:A, D:G, J:X ").EntireColumn.Delete
    End Sub
Private Sub VLOOK_UP()
    Dim I As Integer
    For I = 1 To Split(Worksheets("72 Hr").UsedRange.Address, "$")(4)
        Worksheets("72 Hr").Cells(I, 5).Value = _
        Application.WorksheetFunction.VLookup(Worksheets("72 Hr").Cells(I, 3).Value, _
        Worksheets("3 LTR").Range("A:B"), 2, 1)
    Next I
End Sub
Sub Delete_locals()
With ActiveSheet
    .AutoFilterMode = False
    With Range("C1", Range("C" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*CHS*"
        On Error Resume Next
        .Offset(0).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With

With ActiveSheet
    .AutoFilterMode = False
    With Range("C1", Range("C" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*777*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With

End Sub
Sub Add_Brackets()
With ActiveSheet
        For Each cell In Range("C1", Range("C" & Rows.Count).End(xlUp))
        cell.Value = "(" & cell.Value & ")"
        On Error Resume Next
        Next
End With
End Sub
Sub RemoveFirstThreeCharactersInEachCell()
For Each cell In Range("A1", Range("A65536").End(xlUp))
If Not IsEmpty(cell) Then
cell.Value = Mid(cell, 4, 4)
End If
Next cell
End Sub
Sub moveColumn()

With ActiveSheet
    Columns("C").Cut
    Columns("A").Insert shift:=xlToRight
    Columns("E").Cut
    Columns("B").Insert shift:=xlToRight
End With

End Sub
Sub concatMyData()

For I = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    Cells(I, "A").Value = Cells(I, "A").Value & " " & Cells(I, "B").Value
Next I
Columns(2).Delete

End Sub
Sub No_Departures()
Dim d1 As Date, d2 As Date
For r = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1

d1 = CDate(Split(Cells(r, "D"), " ")(1) & ", " & Split(Cells(r, "D"), " ")(0) & " " & Split(Cells(r, "D"), " ")(2))
d2 = CDate(Split(Cells(r - 1, "D"), " ")(1) & ", " & Split(Cells(r - 1, "D"), " ")(0) & " " & Split(Cells(r - 1, "D"), " ")(2))


If d1 - d2 >= 2 Then

Rows(r).Insert shift:=xlDown

Cells(r, "D") = Format(d1 - 1, "dd mmm yyyy 0700")
Cells(r, "A") = "NO DEPARTURES"
Cells(r, "B") = "N/A"
Cells(r, "C") = "N/A"
End If
Next


End Sub
Sub remove_hidden_Values()
Dim cell As Range
    For Each cell In Selection
        cell.Value = Trim(Replace(cell.Value, Chr(160), Chr(32)))
    Next
End Sub

Sub Subtract7Hours()
    Dim rng As Range
    For Each rng In ActiveSheet.Range("D1", ActiveSheet.Cells(Rows.Count, 4).End(xlUp))
        rng.NumberFormat = "dd mmm yyyy hhmm"
        If IsNumeric(rng) Then
            rng.Value = rng.Value - TimeSerial(7, 0, 0)
        Else
            rng.Value = CDate(Application.Replace(rng, Len(rng) - 1, 0, ":")) - TimeSerial(7, 0, 0)
        End If
    Next rng
End Sub
Sub InsertBlankRow()

    Dim r As Long
    
    For r = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
       If Left(Range("D" & r).Value, 11) <> Left(Range("D" & r - 1).Value, 11) Then
        Rows(r).Resize(1).Insert
        End If
    Next r
End Sub
Sub InsertRows()

    Rows(1).Insert shift:=xlDown
End Sub

   
 Sub Boarder()

    Dim rngDB As Range
    Dim rng2 As Range
    Dim rng As Range, Target As Range
    Dim a As Variant, b As Variant
    Dim myDate As Date
    Dim n As Long
    
    Set rngDB = Range("b1", Range("d" & Rows.Count).End(xlUp))
    
    Set rngDB = rngDB.SpecialCells(xlCellTypeBlanks)
    rngDB.EntireRow.Insert
    Set rng2 = rngDB.Offset(, 2)
    
    a = Array("FLIGHT #", "A/C TYPE", "ROLL CALL")
    b = Array("", "SEAT REL.", "REMARKS")

    With rng2
        .Value = b
        .Font.Size = 12
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        .Borders(xlEdgeBottom).LineStyle = xlContinuous '<~~cell's botton lines style
        .Borders(xlEdgeBottom).Weight = xlMedium
    End With

    With rngDB
        .Value = a
        .Font.Size = 12
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        .Borders(xlEdgeBottom).LineStyle = xlContinuous '<~~cell's botton lines style
        .Borders(xlEdgeBottom).Weight = xlMedium
    End With

    myDate = Date
    n = Date - DateSerial(Year(Date), 1, 0)
    For Each rng In rngDB.Areas
        Set Target = rng.Range("a1").Offset(, -1)
        Target = UCase(Format(myDate, "dddd mmmm d") & " (" & n & ")")
        myDate = myDate + 1
        n = n + 1
    Next rng
    With rngDB.Offset(, -1)
         .Font.Size = 12
        .Font.Name = "Times New Roman"
        .Font.Bold = True
        .Borders(xlEdgeBottom).LineStyle = xlContinuous '<~~cell's botton lines style
        .Borders(xlEdgeBottom).Weight = xlMedium
    End With
    
End Sub
Sub Left_Align()
   
    With Selection
        .Font.Name = "Times New Roman"
        .Font.Size = 10
        .HorizontalAlignment = xlLeft
        .BorderAround xlNone
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlCenter
        .EntireColumn.AutoFit
   End With
   Range("A1:G1").Select
   End Sub


Sub Header()
    Selection.Font.Bold = True
    
    ActiveSheet.PageSetup.CenterHeader = _
        "&""Times New Roman,Bold""&14Flight information is tentative and subject to change without notice" & Chr(10) & "&22DEPARTURES FROM CHS"
        End Sub
  Sub Footer()
    Selection.Font.Bold = False
    
          ActiveSheet.PageSetup.LeftFooter = Format(Time, "hh:mm")

        End Sub
Sub columnWidth_D()
 
    
    Worksheets("72 Hr").Range("F:F").columnWidth = 28
 
End Sub
 Sub UPDATED_BY()

Sheets("72 hR").Range("A34") = "UPDATED BY:"

End Sub
 
 Sub RunAll()

Call delCol
Call VLOOK_UP
Call Delete_locals
Call Add_Brackets
Call RemoveFirstThreeCharactersInEachCell
Call moveColumn
Call concatMyData
Call remove_hidden_Values
Call No_Departures
Call Subtract7Hours
Call InsertBlankRow
Call InsertRows
Call Boarder
Call Left_Align
Call Header
Call Footer
Call columnWidth_D

End Sub
 
Upvote 0
Good weekend, I was testing the code again, and if there is more than one date without flights then your VBA will pick it up. The only time it doesn't is when the dates are consecutive. For example if I have a schedule for 1-10 November and there are no flights on 3 & 7 November it will pick both of them and do as the VBA wants. If I'm missing two consecutive dates like 3 & 4 November it will only flag the second one. If I had numerous consecutive days with no flights I can continually launch that specific VBA until all days without departures are identified; however it would be unfeasible. Are there options to fix this? The rest of my response from the other day is correct. Thank you so much.
 
Upvote 0
try this to fix issue with multiple consecutive dates missing

VBA Code:
Sub Do_it()
Dim d1 As Date, d2 As Date

r = 1
start:

If Cells(r + 1, "D") = "" Then Exit Sub

d1 = CDate(Split(Cells(r, "D"), " ")(1) & ", " & Split(Cells(r, "D"), " ")(0) & " " & Split(Cells(r, "D"), " ")(2))
d2 = CDate(Split(Cells(r + 1, "D"), " ")(1) & ", " & Split(Cells(r + 1, "D"), " ")(0) & " " & Split(Cells(r + 1, "D"), " ")(2))

If d2 - d1 >= 2 Then
    Rows(r + 1).Insert shift:=xlDown
    Cells(r + 1, "D") = Format(d1 + 1, "dd mmm yyyy 0700")
    Cells(r + 1, "A") = "NO DEPARTURES"
    Cells(r + 1, "B") = "N/A"
    Cells(r + 1, "C") = "N/A"
End If

r = r + 1
GoTo start

End Sub
 
Upvote 0
try this to fix issue with multiple consecutive dates missing

VBA Code:
Sub Do_it()
Dim d1 As Date, d2 As Date

r = 1
start:

If Cells(r + 1, "D") = "" Then Exit Sub

d1 = CDate(Split(Cells(r, "D"), " ")(1) & ", " & Split(Cells(r, "D"), " ")(0) & " " & Split(Cells(r, "D"), " ")(2))
d2 = CDate(Split(Cells(r + 1, "D"), " ")(1) & ", " & Split(Cells(r + 1, "D"), " ")(0) & " " & Split(Cells(r + 1, "D"), " ")(2))

If d2 - d1 >= 2 Then
    Rows(r + 1).Insert shift:=xlDown
    Cells(r + 1, "D") = Format(d1 + 1, "dd mmm yyyy 0700")
    Cells(r + 1, "A") = "NO DEPARTURES"
    Cells(r + 1, "B") = "N/A"
    Cells(r + 1, "C") = "N/A"
End If

r = r + 1
GoTo start

End Sub
Your support in this has been nothing less than brilliant, thank you so much. What you provided was spot on, and now I'm at the last piece of my project where I have 22 VBAs. I hope you don't mind if I ask you to slightly tweak the code you already provided? The code you provided works as it should when the Column is in a "General" format. However, I had two VBAs I had to use: one to remove hidden spaces and special characters when I copied data from an external source (First VBA). Then I had to subtract 7 hours to get local time from GMT, (Second VBA). These two resulted in a format change to a Custom Format "dd mmm yyyy hhmm" leading me to receive an error if I use your formula after these two VBAs. I hoped I could just run yours before the VBAs I listed, but there could be flights that get rolled back to the previous date where the "NO DEPARTURES" was inserted. Simply put I need help with your VBA so it will go into a Custom Format already described. Thank you so much!

VBA Code:
Sub remove_hidden_Values()
Dim cell As Range
    For Each cell In Selection
        cell.Value = Trim(Replace(cell.Value, Chr(160), Chr(32)))
    Next
End Sub

VBA Code:
Sub Subtract7Hours()
    Dim rng As Range
    For Each rng In ActiveSheet.Range("D1", ActiveSheet.Cells(Rows.Count, 4).End(xlUp))
        rng.NumberFormat = "dd mmm yyyy hhmm"
        If IsNumeric(rng) Then
            rng.Value = rng.Value - TimeSerial(7, 0, 0)
        Else
            rng.Value = CDate(Application.Replace(rng, Len(rng) - 1, 0, ":")) - TimeSerial(7, 0, 0)
        End If
    Next rng
End Sub
 
Upvote 0
Hello again, I wrote to you yesterday in how the formula seems to have a bug after I run to other Macros. I did a fair amount of research in the matter and I found this VBA below. It seems to do the work, but for the life of me I don't know why it wants to insert a row when there clearly it's not missing a date. In the image you can see basically it does insert the date where there is a missing day. Like before I would like it to have the date but the time would be 0000. Column A would be "NO DEPARTURES", Columns B & C would be "N/A" and Column D as described. I liked your formula better because it was so much shorter, just wished it worked after the other VBAs. Thank you so much!

VBA Code:
Sub InsertMissingRows()
  Dim X As Long, LastRow As Long, Difference As Long
  Const OrderColumn As String = "D"
  Const StartRow As Long = 2
  LastRow = Cells(Rows.Count, OrderColumn).End(xlUp).Row
  For X = LastRow To StartRow + 1 Step -1
    Difference = Cells(X, OrderColumn).Value - Cells(X - 1, OrderColumn)
    If Difference > 1 Then
      Rows(X).Resize(Difference - 1).Insert
    End If
  Next
  LastRow = Cells(Rows.Count, OrderColumn).End(xlUp).Row
  Cells(StartRow, OrderColumn).Resize(LastRow - StartRow + 1). _
        SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=1+R[-1]C"
  Columns("D").Value = Columns("D").Value
End Sub
insert blank.JPG
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,973
Members
448,933
Latest member
Bluedbw

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