Vba to distribute days across multiple years

motherindia

Board Regular
Joined
Oct 15, 2015
Messages
218
Before Running macro
The sheeet1 contains following data. I need number of days between 1-4-2014 40 31-3-2019 which should be displayed in shet2
Cust IDSt DateEnd Date
101-04-201230-04-2016
101-04-201830-04-2020
After running macro sheet2 sould have follong out put
Years
01-04-201401-04-201501-04-201601-04-201701-04-2018
Cust ID31-03-201531-03-201631-03-201731-03-201831-03-2019
1365366300365

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

Regards,
motherindia
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hello.

There doesn't seem to be any logic between sheet1 and sheet2. Can you clarify. The dates are totally different. Or am I missing something?
 
Upvote 0
Thanks for quick responses.
The example given above only to get 5 years between 1st apr to 31st March.

Regards,
motherindia
 
Upvote 0
Hi Team,
I need to capture number of days that falls between start and end date for specific 5 years. For example I need to capture number of days that falls
between 1st Apr 2014 to 31st Mar 2019 for cust ID 1 in the following example
Cust IDSt DateEnd Date
101-04-201230-04-2016
101-04-201830-04-2020
After running macro sheet2 should have following out put
Cust IDYears
01-04-201401-04-201501-04-201601-04-201701-04-2018
31-03-201531-03-201631-03-201731-03-201831-03-2019
Nof Days
1365366300365

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


Regards,
motherindia
 
Upvote 0
Hello,

Can get the dates, as per your example.

Code:
Sub years()
    MY_CUST = Range("A2").Value
    MY_END = Year(Range("C3").Value) - 1
    MY_START = MY_END - 5
    With Sheets("Sheet2")
        .Range("A1").Value = "Cust ID"
        .Range("A2").Value = MY_CUST
        .Range("B1").Value = "Years"
        For MY_COLS = 2 To 6
            .Cells(2, MY_COLS).Value = DateValue("1/4/" & MY_START)
            .Cells(3, MY_COLS).Value = DateValue("31/3/" & MY_START + 1)
            MY_START = MY_START + 1
        Next MY_COLS
    End With
End Sub

Not sure how you get the Nof days though. There doesn't seem to be any logic to the numbers in the sample.
 
Upvote 0
Hello Sir,
Thanks a lot for quick responses;

Problem :
Cust IDSt DateEnd DatePlace
101-04-201230-04-2016Delhi
101-04-201830-04-2020Mumbai

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

Let me give another example . In the above example I have added another column called Place so that it is easy for understanding the requirement.
Suppose if I want to know how many days I was in Delhi and in Mumbai between year 2014 to 2020. However, the days should be calculated between Apr to March.
That is the number of days in Delhi for the year 2014-15 ( 01.04.2014 to 31.03.2015) is 365
That is the number of days in Delhi for the year 2014-15 ( 01.04.2015 to 31.03.2016) is 366
That is the number of days in Delhi for the year 2014-15 ( 01.04.2016 to 31.03.2017) is 30
That is the number of days in Delhi for the year 2014-15 ( 01.04.2017 to 31.03.2018) is 0
That is the number of days in Mumbai for the year 2014-15 ( 01.04.2014 to 31.03.2015) is 365
Hence the out put should be as follows for the number of days
The macro can prompt for input the date range to capture number of days (Between 1st Apr to 31st Mar) eg: between 2014 to 2020.
Cust ID2014-152015-162016-172017-182018-192019-20
1365366300365366

<colgroup><col><col><col><col span="8"><col span="3"></colgroup><tbody>
</tbody>


Regards,
motherindia
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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