Excel VBA or Formula to Split Period of Months into Individual Month

zakkair

New Member
Joined
Jan 29, 2013
Messages
39
Hi guys,

I have a sheet with data similar to the following table:

Name
Location
Start Date
End Date
Days
John
Canada
January 1, 2013
February 24, 2013
55
Jack
US
February 18, 2013
March 23, 2013
34

<TBODY>
</TBODY>


Is it possible to create an Excel formula or VBA to split this set of data into individual months such that it looks like this

Name
Location
Start Date
End Date
Days
John
Canada
January 1, 2013
January 31, 2013
31
John
Canada
February 1, 2013
February 24, 2013
24
Jack
US
February 18, 2013
February 28, 2013
11
Jack
US
March 1, 2013
March 23, 2013
23

<TBODY>
</TBODY>


Thanks for any help I can get!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This macro should do the trick. Be sure those dates are formatted as dates and recognized as such
Code:
Sub adjustmonth()
    Dim I As Integer
    Dim lastrow As Integer
    Dim wf As WorksheetFunction
    
    Set wf = Application.WorksheetFunction
    
    lastrow = Range("A1").End(xlDown).Row
    
    For I = lastrow To 2 Step -1
        a = Range("C" & I).Value
        b = Range("D" & I).Value
        diff = (Year(b) - Year(a)) * 12 + Month(b) - Month(a)
        If diff > 0 Then
            Range("D" & I).Value = wf.EoMonth(a, 0)
            Range("E" & I).Value = Range("D" & I).Value - Range("C" & I).Value + 1
                
            For k = 1 To diff
                Rows(I + k).Resize(1).Insert
                Range("A" & I + k).Value = Range("A" & I + k - 1).Value
                Range("B" & I + k).Value = Range("B" & I + k - 1).Value
                Range("C" & I + k).Value = Range("D" & I + k - 1).Value + 1
                If k <> diff Then
                    Range("D" & I + k).Value = wf.EoMonth(Range("C" & I + k).Value, 0)
                Else
                    Range("D" & I + k).Value = b
                End If
                Range("E" & I + k).Value = Range("D" & I + k).Value - Range("C" & I + k).Value + 1
            Next k
        End If
    Next I
End Sub
 
Upvote 0
You are welcome, Was quite a learning experience for me :)
 
Upvote 0
Just another quick question regarding the code,

What would I edit if there's another two columns F-G so that they repeat like column A, B, and C when you use the macro?
 
Upvote 0
Maybe not the most efficient of ways but you could add 2 lines like

Rich (BB code):
Range("F" & I + k).Value = Range("F" & I + k - 1).Value
Rich (BB code):
Range("G" & I + k).Value = Range("G" & I + k - 1).Value

Add them underneath the lines that copy for column A, B and C
 
Upvote 0
This works amazingly. However, can you alter this code to split the dates in 12 months with beginning month of April 1st.

It would be great help.
 
Upvote 0
This works amazingly. However, can you alter this code to split the dates in 12 months with beginning month of April 1st.

It would be great help.

This post is almost 3 years old :) I probably cant remember how the code even works

Can you give an example of what you really require
 
Upvote 0
Essentially splitting long periods into 12 months with start of April 1st.

I.e.

NameLocationStart DateEnd DateMonths
JohnCanada1/1/20162/24/202050
JackUS2/18/20173/23/202374
Results
NameLocationStart DateEnd DateMonths
JohnCanada1/1/20163/31/20163
JohnCanada4/1/20163/31/201712
JohnCanada4/1/20173/31/201812
JohnCanada4/1/20183/31/201912
JohnCanada4/1/20182/24/202023
JackUS2/18/20173/31/20171
JackUS4/1/20173/31/201812
JackUS4/1/20183/31/201912
JackUS4/1/20193/31/202012
JackUS4/1/20203/31/202112
JackUS4/1/20213/31/202212
JackUS4/1/20223/23/202312

<colgroup><col span="2"><col><col><col></colgroup><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
This is superb, thank you so much Momentman.
It gives me a a curiosity to JPAvila request. i tried your code and come out with the below code.

VBA Code:
[
Sub adjustYear()
    Dim I As Integer
    Dim lastrow As Integer
    Dim wf As WorksheetFunction
    
    Worksheets("Sheet2").Select
    Set wf = Application.WorksheetFunction
    
    lastrow = Range("A1").End(xlDown).Row
    
    For I = lastrow To 2 Step -1
        a = Range("C" & I).Value
        b = Range("D" & I).Value
        diff = (year(b) - year(a)) ' * 12 ' + Month(b) - Month(a)
        
        If diff > 0 Then
            Range("D" & I).Value = wf.EDate(a, 12) - 1
            Range("E" & I).Value = Range("D" & I).Value - Range("C" & I).Value + 1
                
            For k = 1 To diff
                Rows(I + k).Resize(1).Insert
                Range("A" & I + k).Value = Range("A" & I + k - 1).Value
                Range("B" & I + k).Value = Range("B" & I + k - 1).Value
                Range("C" & I + k).Value = Range("D" & I + k - 1).Value + 1
                If k <> diff Then
                    Range("D" & I + k).Value = wf.EDate(Range("C" & I + k).Value, 12) - 1
                Else
                    Range("D" & I + k).Value = b
                End If
                Range("E" & I + k).Value = Range("D" & I + k).Value - Range("C" & I + k).Value + 1
            Next k
        End If
    Next I
End Sub
]
 
Upvote 0

Forum statistics

Threads
1,215,706
Messages
6,126,336
Members
449,310
Latest member
zztt388

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