Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default overflow issues...PLEASE HELP!!!

Hi everyone,

I have written a small program that calculates daily compounded interest and
keep having a problem with an overflow error. My interest rate variables get
too big and crash the program. Isn't there a way to limit how big the
numbers get??? Please advise...


Sub interestcalculation()


Dim t As Integer

Dim v_startingprinciple As Double
Dim v_rounddownmultiple As Double
Dim v_actualinterestrate As Single
Dim v_dailyinterest As Single
Dim v_initialprinint As Double
Dim v_accumprinint As Double
Dim v_previoustest As Variant
Dim v_rate As Single
Dim v_remainingdaysinperiod As Double




'NOTES:
'FY2006 = 0.0057
'FY2007 = 0.0107
'FY2008 = 0.0145
'FY2009 = 0.0119

'Manually set variables for execution:
v_startingprinciple = 200
v_rate = 0.000057
v_remainingdaysinperiod = 26


v_rounddownmultiple =
(Application.WorksheetFunction.RoundDown(v_startin gprinciple, -2) / 100)

v_actualinterestrate = v_rounddownmultiple * v_rate

v_dailyinterest = v_startingprinciple * v_actualinterestrate

v_initialprinint = v_startingprinciple + v_dailyinterest


v_accumprinint = 0

For t = 2 To v_remainingdaysinperiod

v_rounddownmultiple =
(Application.WorksheetFunction.RoundDown(v_initial prinint, -2) / 100)

v_actualinterestrate = v_rounddownmultiple *
v_rate

v_dailyinterest = v_initialprinint *
v_actualinterestrate

v_initialprinint = v_initialprinint +
v_dailyinterest



Next t

'select the cell you'd like to output the number in BEFORE you run
the analysis!
ActiveCell.Value = v_initialprinint




End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default overflow issues...PLEASE HELP!!!

For integral number (whole numbers, no fractional part), use Longs
which have a range of approximately +/- 2 billion. For fractional
numbers, use Doubles, which have a range of approx +/- 10^308,
although Doubles only support accuracy to 15 digits. Everything else
is rounded.

There is no advantage to using Integer or Single type variables.

Note also that the compiler uses the smallest variable types it can
when doing calculations. This can lead to strange errors. For example,

Debug.Print 10 * 3300 / 10

equals 3300, and both 10 and 3300 are well within the bounds of an
Integer type. But the running this will cause an overflow error. Why?
Because the intermediate result (10*3300) does exceed the limits of an
integer. You can force the compiler to use Longs by including the CLng
function on one variable:

Debug.Print CLng(10) * 3300 / 10

or by using the Long type declaration character on one variable:

Debug.Print 10& * 3300 / 10

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Fri, 20 Mar 2009 07:10:01 -0700, Steve P
wrote:

Hi everyone,

I have written a small program that calculates daily compounded interest and
keep having a problem with an overflow error. My interest rate variables get
too big and crash the program. Isn't there a way to limit how big the
numbers get??? Please advise...


Sub interestcalculation()


Dim t As Integer

Dim v_startingprinciple As Double
Dim v_rounddownmultiple As Double
Dim v_actualinterestrate As Single
Dim v_dailyinterest As Single
Dim v_initialprinint As Double
Dim v_accumprinint As Double
Dim v_previoustest As Variant
Dim v_rate As Single
Dim v_remainingdaysinperiod As Double




'NOTES:
'FY2006 = 0.0057
'FY2007 = 0.0107
'FY2008 = 0.0145
'FY2009 = 0.0119

'Manually set variables for execution:
v_startingprinciple = 200
v_rate = 0.000057
v_remainingdaysinperiod = 26


v_rounddownmultiple =
(Application.WorksheetFunction.RoundDown(v_starti ngprinciple, -2) / 100)

v_actualinterestrate = v_rounddownmultiple * v_rate

v_dailyinterest = v_startingprinciple * v_actualinterestrate

v_initialprinint = v_startingprinciple + v_dailyinterest


v_accumprinint = 0

For t = 2 To v_remainingdaysinperiod

v_rounddownmultiple =
(Application.WorksheetFunction.RoundDown(v_initia lprinint, -2) / 100)

v_actualinterestrate = v_rounddownmultiple *
v_rate

v_dailyinterest = v_initialprinint *
v_actualinterestrate

v_initialprinint = v_initialprinint +
v_dailyinterest



Next t

'select the cell you'd like to output the number in BEFORE you run
the analysis!
ActiveCell.Value = v_initialprinint




End Sub

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
Overflow jmdaniel Excel Programming 4 September 5th 06 07:09 PM
Overflow Erik Beck Jensen Excel Programming 2 December 13th 05 10:11 AM
Overflow when translating from C++ HELP!! hunting Excel Programming 6 April 24th 04 08:18 PM
VBA overflow Tom Ogilvy Excel Programming 3 September 2nd 03 09:04 PM
VBA overflow Don Guillett[_4_] Excel Programming 2 September 2nd 03 04:19 PM


All times are GMT +1. The time now is 05:50 PM.

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

About Us

"It's about Microsoft Excel"