Results 1 to 4 of 4

UDF rounding currency funny

Threaded View

  1. #1
    Registered User
    Join Date
    04-02-2009
    Location
    Washington, USA
    MS-Off Ver
    Excel 2003
    Posts
    12

    UDF rounding currency funny

    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!
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1