Follow up on previous question (inserting current missing date)

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This past Saturday I posted a question about inserting a “current” date if missing. I provided a VBA which inserts missing dates based in Column D. Should I try to work it into the current formula which works perfect besides not inserting the current date, or should I create another macro? As the VBA states NO DEPARTURES in Column A, N/A in Columns B & C and in Column D the current missing date format 15 NOV 2020 0000 using the image as a guide. Thank you so much.

VBA Code:
Option Explicit

Sub InsertMissingDates()
    ' 111
   
    Dim NextDate    As Variant
    Dim CellVal     As Variant
    Dim R           As Long                     ' loop counter: Rows
   
    R = Cells(Rows.Count, "D").End(xlUp).Row
    NextDate = CellDate(Cells(R, "D"))
    If NextDate = vbError Then Exit Sub
   
    ' bottom rows must be inserted before top rows
    For R = R - 1 To 2 Step -1
        CellVal = CellDate(Cells(R, "D"))
        If CellVal = vbError Then Exit For       ' exit if date can't be recognised
       
        Do While Int(CDbl(CellVal)) < Int(CDbl(NextDate - 1))
            Rows(R + 1).Insert Shift:=xlDown
            With Cells(R + 1, "D")
                .Value = Int(CDbl(NextDate - 1))
                .NumberFormat = "dd mmm yyyy hhmm"
                .HorizontalAlignment = xlLeft
            End With
            Cells(R + 1, "A").Value = "NO DEPARTURES"
            Cells(R + 1, "B").Value = "N/A"
            Cells(R + 1, "C").Value = "N/A"
            NextDate = NextDate - 1
        Loop
        NextDate = CellVal
    Next R
End Sub

Private Function CellDate(Cell As Range) As Variant
    ' 111
    ' return vbError if cell's value couldn't be converted to a date
   
    Dim Fun         As Variant              ' function return value
    Dim CellVal     As Variant
    Dim Sp()        As String
   
    CellVal = Cell.Value
    If IsDate(CellVal) Then
        Fun = CDate(CellVal)
    Else
        Sp = Split(CellVal, " ")
           
        If UBound(Sp) = 3 Then
            Sp(3) = Right("0000" & Sp(3), 4)
            Sp(3) = Left(Sp(3), 2) & ":" & Right(Sp(3), 2)
            On Error Resume Next
            Fun = CDate(Join(Sp))
        End If
    End If
    If VarType(Fun) <> vbDate Then
            MsgBox """" & CellVal & """ in row " & Cell.Row & vbCr & _
                   "couldn't be converted to a date.", _
                   vbInformation, "Data format error"
            Fun = vbError
    End If
    CellDate = Fun
End Function
 

Attachments

  • 5D5D483C-57D4-4C23-BEE6-B51E2B14D16C.jpeg
    5D5D483C-57D4-4C23-BEE6-B51E2B14D16C.jpeg
    24.4 KB · Views: 3

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

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