NPV function with variable rates

Ozzman

New Member
Joined
Jul 12, 2011
Messages
17
Hello all! I am attempting to create a function similar to Excel's NPV() function, but would like it to use more than one interest rate. I have attempted this in VBA using several sources and have coded the following:
Function Net(r, v, t) x1 = r.Count x2 = v.Count x3 = t.Count If x1 <> x2 <> x3 Then Net = CVErr(xlErrNum) End If Net = 0 For i = 1 To x1 For j = 1 To x2 For k = 1 To x3 Net = Net + v(j) / (1 + r(i)) ^ t(k) Next k Next j Next i Net = WorksheetFunction.Sum(r, v, t) End Function
I know I am missing a line or two of code that would make this work. Right now, this function is off by about 700 dollars with the example that I use. I am using a range of rates, values and times (in that order)
 
But there isn't anything at that point in the code that would trip an error.

Well, it trips. I opened up a brand new module and pasted it in and I got an error. I have everything you had in the code you gave (minus the 'Exit Function') and changed the 'As Double' to 'As Range' and I get a value. I insert the 'Exit Function' and I get a #NUM! in the cell
think i found a problem in your code then. you're using arrays v(i) and such. Arrays start with ZERO, not 1. so you're missing out on the first number of all of your arrays. change all of your
Code:
For i = 1 to x1
into
Code:
For i = 0 to x1 -1 step 1
That should give you different answers.

I'll try that
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I might get OCD about this and try it tonight.... would you please post a sample of your work, and the desired outcome? Use the Excel Jeanie that is listed in the sticky.
 
Upvote 0
I might get OCD about this and try it tonight.... would you please post a sample of your work, and the desired outcome? Use the Excel Jeanie that is listed in the sticky.

That seems too complicated and I don't have enough time to do that. I will post a screenshot instead:
unledbyr.jpg



Okay, the custom function is in the 5th column (net(E6:E15,etc). The regular ol' present value of these amounts is given in the column to the left of the column with the Net() function. The difference between these amounts is in the very last column. Feel free to copy these values and see how far off it is. First three columns are rates, values and time respectively. </br></br>
 
Upvote 0
question 1. What part of the sheet am I looking at? (what range)
and 2. What is the desired outcome? (as you've done by hand)
 
Upvote 0
here you go
Code:
Function Net(ByVal r As Range, ByVal v As Range, ByVal t As Range)
    If r.Count <> v.Count Or r.Count <> t.Count Then
        Net = "ERROR"
    Else
        For i = 1 To v.Count
            Net = Net + (v(i) / ((1 + r(i)) ^ t(i)))
        Next i
    End If
End Function
 
Upvote 0
This one only requires you to input ONE range

i.e.
Code:
=Net(A1:C10)
Code:
Function Net(ByVal rvt As Range)
    For i = 1 To rvt.Rows.Count
        Net = Net + (rvt(i, 2) / ((1 + rvt(i, 1)) ^ rvt(i, 3)))
    Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,225,169
Messages
6,183,318
Members
453,155
Latest member
joncaxddd

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