OK, I have developed a UDF that takes a total cost applied over a period of time and breaks that cost down into cost per month. It is working great, except for one problem. Excel is rounding out anything less than $1, so that if you have a cost that is not exactly divisible by the number of days in the period, you get a discrepancy between the broken-down costs and the total cost. I have attached what I have so far, and here is the code for the UDF:
Public Function CPM(StartDate As Date, EndDate As Date, TotalCost As Currency, CurMonth As Date, NextMonth As Date)
Dim CPD As Integer
CPD = TotalCost / (EndDate - StartDate)
If StartDate < NextMonth And EndDate >= CurMonth Then
If EndDate >= CurMonth And EndDate < NextMonth Then
If (EndDate - StartDate) <= (NextMonth - CurMonth) And StartDate > CurMonth And EndDate >= CurMonth Then
CPM = TotalCost
ElseIf (EndDate - StartDate) <= (NextMonth - CurMonth) And StartDate <= CurMonth And EndDate < NextMonth Then
CPM = -(CurMonth - EndDate) * CPD
Else
CPM = (EndDate - CurMonth) * CPD
End If
ElseIf StartDate >= CurMonth And StartDate < NextMonth Then
CPM = (NextMonth - StartDate) * CPD
Else
CPM = (NextMonth - CurMonth) * CPD
End If
Else
CPM = 0
End If
End Function
Any help would be awesome! Thanks!
Bookmarks