UDF to count days between two dates

j.saravia

New Member
Joined
Oct 11, 2005
Messages
4
Hello,

If anyone could help me with this UDF, I will really appreciate it.

At work, I use a table that counts the # of days between a Contract Start Date and the Contract End Date.

In the UDF I've been able to write so far (See below) , I have no issues with the first year as long as the function is counting days between the same calendar year. The problems start when I need to break the # of days among multimple years (See link below to sample table).

For example, if an contract goes from 01-Jul-06 to 30-Jun-07, how do I make the function count only the days belong to calendar year 2007 under the 2007 Column?

Link to sample table image:
http://docs.google.com/Present?docID=ddr9b8g_2hp5k7pfg&revision=_latest&start=0&theme=blank&cwj=true

UDF (under construction)
----------------------------------------------------------------------------
Function ContractDays2006(OrderStartDate As Date, OrderEndDate As Date)

Dim StartDate As Date
Dim EndDate As Date

'38718 = 01-Jan-06
'39082 = 31-Dec-06
'39083 = 01-Jan-07
'39447 = 31-Dec-07

If OrderStartDate > 39082 Then

ContractDays2006 = 0

Exit Function

Else

StartDate = OrderStartDate

End If

If OrderEndDate > 39082 Then

EndDate = 39083

Else

EndDate = OrderEndDate

End If

ContractDays2006 = EndDate - StartDate

End Function
-----------------------------------------------------------------------

Thank you.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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