Vba to distribute days across multiple years

motherindia

Board Regular
Joined
Oct 15, 2015
Messages
216
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
 

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,615
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?
 

motherindia

Board Regular
Joined
Oct 15, 2015
Messages
216
Thanks for quick responses.
The example given above only to get 5 years between 1st apr to 31st March.

Regards,
motherindia
 

motherindia

Board Regular
Joined
Oct 15, 2015
Messages
216
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
 

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,615
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.
 

motherindia

Board Regular
Joined
Oct 15, 2015
Messages
216
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
 

Forum statistics

Threads
1,077,614
Messages
5,335,253
Members
399,009
Latest member
twcaddell

Some videos you may like

This Week's Hot Topics

Top