Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Function works except with January and February?

If I use 03/01/05 as the start date the function works. However, if I
use 02/01/05 or 01/01/05 I get #Value!

What am I missing?

stan


Public Function TRM(compYear As Integer, sDate As Date, numUnits As
Integer, trans As Currency) As Double

Const yearAmt = 36000
Const monthAmt = 3000
Const PRN = 2

Dim tAmt As Currency

Dim calcYear As Integer
calcYear = Year(sDate)

Dim eDate As Date
eDate = CDate("12/31/" & CStr(calcYear))

Dim pAmt As Currency
Dim AllAmt As Currency

Dim numMonths As Integer
numMonths = CInt(DateDiff("m", sDate, eDate)) + 1


tAmt = numMonths * monthAmt
If compYear = calcYear Then
pAmt = numUnits * tAmt
Else
pAmt = yearAmt * numUnits

End If
If trans < pAmt Then
TRM = 0

Else
TRM = (trans - pAmt) * PRN
End If

End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Function works except with January and February?

I am getting an overflow error on the line tAmt = numMonths * monthAmt.
NumMonths is Dim'ed as an integer and monthAmt is a constant, so Excel is
using integer multiplication before trying to store the result in your tAmt
(Currency) variable - leading to an overflow when the result is 32767,
which happens when numMonths is 11 (so it bombs for Jan, Feb).

Solution: use a Long or Currency variable for numMonths, or write your
formula as tAmt = CCur(numMonths) * monthAmt

" wrote:

If I use 03/01/05 as the start date the function works. However, if I
use 02/01/05 or 01/01/05 I get #Value!

What am I missing?

stan


Public Function TRM(compYear As Integer, sDate As Date, numUnits As
Integer, trans As Currency) As Double

Const yearAmt = 36000
Const monthAmt = 3000
Const PRN = 2

Dim tAmt As Currency

Dim calcYear As Integer
calcYear = Year(sDate)

Dim eDate As Date
eDate = CDate("12/31/" & CStr(calcYear))

Dim pAmt As Currency
Dim AllAmt As Currency

Dim numMonths As Integer
numMonths = CInt(DateDiff("m", sDate, eDate)) + 1


tAmt = numMonths * monthAmt
If compYear = calcYear Then
pAmt = numUnits * tAmt
Else
pAmt = yearAmt * numUnits

End If
If trans < pAmt Then
TRM = 0

Else
TRM = (trans - pAmt) * PRN
End If

End Function

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need a february 29th row, but only if the day of year actually exists FatBytestard Excel Worksheet Functions 17 June 9th 09 12:50 PM
calculating due dates and february differences [email protected] Excel Discussion (Misc queries) 8 February 5th 08 06:19 PM
Date plus 1 year (february problem) Jessica Excel Worksheet Functions 12 June 28th 06 02:50 PM
When February 29 is date? jj4446 Excel Worksheet Functions 1 January 13th 06 04:11 PM
using date function, month shows as January when i type (12) hsas Excel Discussion (Misc queries) 3 June 17th 05 07:15 PM


All times are GMT +1. The time now is 11:23 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"