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:

1/31/20183/31/2014/30/20187/31/20189/30/2018
5000025000400003000045000
55000300004500015000030000
4000045000400002500050000

<tbody>
</tbody>

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:

1/31/182/28/183/31/184/30/185/31/186/30/187/31/188/31/189/30/1810/31/1811/30/1812/31/18
5000025000400003000045000
55000300004500015000030000
4000045000400002500050000

<tbody>
</tbody>

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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,215,104
Messages
6,123,113
Members
449,096
Latest member
provoking

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