compounded return for each loop problem

Chronicles84

New Member
Joined
Apr 8, 2017
Messages
7
Hi,

I'm trying to create a UDF function to calculate a return on a principal amount, with different interest rates for different periods. I know there are built in excel functions that can do this, but I'm trying to learn more about vba through this simple example.

Here is what I've written so far:

Function compounded_returns(Principal As Double, Rets As Range) As Double


Code:
Dim vbCell As Range
compounded_returns = 0


    For Each vbCell In Rets
        compounded_returns = Principal * (1 + vbCell.Value)
    Next vbCell


compounded_returns = compounded_returns


End Function

The problem is when executed, the UDF only returns the principal * the last range. The difference between the UDF and Excel's function is shown below:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Principal[/TD]
[TD]Rets[/TD]
[/TR]
[TR]
[TD]10,000[/TD]
[TD]5%[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]6%[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]7%[/TD]
[/TR]
[TR]
[TD]Excel's FVSchedule[/TD]
[TD]11909.1[/TD]
[/TR]
[TR]
[TD]UDF[/TD]
[TD]10700[/TD]
[/TR]
</tbody>[/TABLE]

Can anyone point out how i need to modify the UDF? your help/advice would be appreciated
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
argh, the copy and paste did not go so well. Obviously, the full code includes the original function call

Code:
Function compounded_returns(Principal As Double, Rets As Range) As Double

Dim vbCell As Range
compounded_returns = 0




    For Each vbCell In Rets
        compounded_returns = Principal * (1 + vbCell.Value)
    Next vbCell


compounded_returns = compounded_returns
 
Upvote 0
Welcome to Mr Excel

See if this does what you need

Code:
Function compounded_returns(Principal As Double, Rets As Range) As Double
    Dim vbCell As Range, dbMult As Double
    
    dbMult = 1
    For Each vbCell In Rets
        dbMult = dbMult * (1 + vbCell.Value)
    Next vbCell
    compounded_returns = Principal * dbMult
End Function

M.
 
Last edited:
Upvote 0
Hi Marcelo

Thank you very much. Yes, this has the desired effect - I will have to this solution more to know for the future - thank you again!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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