Insert Columns For Missing Dates

sugargenius

New Member
Joined
Sep 16, 2009
Messages
40
I have report that comes out of a legacy system with fiscal period end dates as column headings and budget values below:

[TABLE="width: 500"]
<tbody>[TR]
[TD]1/31/2018[/TD]
[TD]3/31/201[/TD]
[TD]4/30/2018[/TD]
[TD]7/31/2018[/TD]
[TD]9/30/2018[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD]25000[/TD]
[TD]40000[/TD]
[TD]30000[/TD]
[TD]45000[/TD]
[/TR]
[TR]
[TD]55000[/TD]
[TD]30000[/TD]
[TD]45000[/TD]
[TD]150000[/TD]
[TD]30000[/TD]
[/TR]
[TR]
[TD]40000[/TD]
[TD]45000[/TD]
[TD]40000[/TD]
[TD]25000[/TD]
[TD]50000[/TD]
[/TR]
</tbody>[/TABLE]

I need macro that loops through each period based on a arbitrary start end date and inserts any missing periods in above table.

Using the table above and a start date of 1/31/2018 and a finish date of 12/31/2018, the result would look like:

[TABLE="width: 500"]
<tbody>[TR]
[TD]1/31/18[/TD]
[TD]2/28/18[/TD]
[TD]3/31/18[/TD]
[TD]4/30/18[/TD]
[TD]5/31/18[/TD]
[TD]6/30/18[/TD]
[TD]7/31/18[/TD]
[TD]8/31/18[/TD]
[TD]9/30/18[/TD]
[TD]10/31/18[/TD]
[TD]11/30/18[/TD]
[TD]12/31/18[/TD]
[/TR]
[TR]
[TD]50000[/TD]
[TD][/TD]
[TD]25000[/TD]
[TD]40000[/TD]
[TD][/TD]
[TD][/TD]
[TD]30000[/TD]
[TD][/TD]
[TD]45000[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]55000[/TD]
[TD][/TD]
[TD]30000[/TD]
[TD]45000[/TD]
[TD][/TD]
[TD][/TD]
[TD]150000[/TD]
[TD][/TD]
[TD]30000[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]40000[/TD]
[TD][/TD]
[TD]45000[/TD]
[TD]40000[/TD]
[TD][/TD]
[TD][/TD]
[TD]25000[/TD]
[TD][/TD]
[TD]50000[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Here's what I have so far (not working):

Code:
Option Base 1
Public Sub datetest()
    Dim startDate As Date
    Dim aPeriods() As Date
    Dim iPeriods As Integer
    Dim rngPeriod As Range
    Dim iStartCol As Integer
    Dim iPeriodRow As Integer
    Dim xyz As Date
    startDate = CDate("1/31/2018")
    endDate = CDate("12/31/2018")
    currentDate = startDate
    iPeriods = 0
    Do While currentDate <= endDate
        iPeriods = iPeriods + 1
        ReDim Preserve aPeriods(iPeriods)
        aPeriods(iPeriods) = currentDate
        currentDate = DateSerial(Year(currentDate), Month(currentDate) + 2, 0)
    Loop
   
    iStartCol = 1
    iPeriodRow = 5
    For i = 1 To iPeriods
        Debug.Print Cells(iPeriodRow, iStartCol + i)
        xyz = CDate(Cells(iPeriodRow, iStartCol + i))
        Debug.Print xyz = aPeriods(i)
        If xyz <> aPeriods(i) Then
            Columns(iStartCol + i).Insert Shift:=xlToLeft
        End If
        
    Next i
    
End Sub
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try with this:


Code:
Sub Insert_Columns()
    'Insert Columns For Missing Dates
    Application.ScreenUpdating = False
    uc = Cells(5, Columns.Count).End(xlToLeft).Column
    mes = 12
    For j = uc To 1 Step -1
        MDate = DateSerial(Year(Date), mes, Day(DateSerial(Year(Date), mes + 1, 1) - 1))
        If Cells(5, j).Value <> MDate Then
            Columns(j + 1).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            Cells(5, j + 1).Value = MDate
            j = j + 1
        End If
        mes = mes - 1
        If mes = 0 Then Exit Sub
    Next
    For j = mes To 1 Step -1
        MDate = DateSerial(Year(Date), j, Day(DateSerial(Year(Date), j + 1, 1) - 1))
        Columns(1).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Cells(5, 1).Value = MDate
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
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