• Status: Solved
• Priority: Medium
• Security: Public
• Views: 958

# Need IRR Function similar to Excel and VB for ASP

Hello all.  I am trying to calculate an IRR function in an ASP page I have.  I need to pass in 4 values and get back the internal rate of return as a %.  For example in VB:

Dim Guess, RetRate, Values(4) As Double
Dim Fmt, Msg As String
Guess = 0.1  ' Guess starts at 10 percent.
Fmt = "#0.00"   ' Define percentage format.
Values(0) = -2465399   ' Business start-up costs.
' Positive cash flows reflecting income for four successive years.
Values(1) = 1725956: Values(2) = 1686970
Values(3) = 1599299
RetRate = IRR(Values, Guess) * 100   ' Calculate internal rate.
Msg = "The internal rate of return for these four cash flows is "
Msg = Msg & Format(RetRate, CStr(Fmt)) & " percent."
MsgBox (Msg)   ' Display internal return rate.

Gives me back 46.75

Any idea how I can complete this in ASP?  Thanks all

0
sbornstein2
• 2
• 2
1 Solution

Commented:
Actually the VBScript for ASP is very similar to straight VB.  It won't do some of the formatting, though.  You can probably convert the answer to a string and manipulate it to two decimal points that way.  That sounds kinda "klunky", though.  If any one else knows a better way to format numbers with ASP/VBScript please respond.

I don't know what you are doing in the IRR function so I just played around with the numbers you provided a little.  The MsgBox and Alert functions are similar so i included both here, too.

<HTML>
<BODY>
<script language="VBScript">
Dim Guess
Dim RetRate
Dim Values(4)
Dim Fmt
Dim MsgStr
Guess = 0.1  ' Guess starts at 10 percent.
Fmt = "#0.00"   ' Define percentage format.
Values(0) = -2465399   ' Business start-up costs.
' Positive cash flows reflecting income for four successive years.
Values(1) = 1725956
Values(2) = 1686970
Values(3) = 1599299
RetRate = IRR(Values, Guess) * 100   ' Calculate internal rate.
MsgBox ("The internal rate of return for these four cash flows is " & RetRate & " percent.")
Alert ("The internal rate of return for these four cash flows is " & RetRate & " percent.")

function IRR(Values, Guess)
'Do what you really want here........
IRR = Values(0)/(Values(1) + Values(2) + Values(3))
end function
</script>
</BODY>
</HTML>
0

Author Commented:
Hey danataylor thanks so much for responding I was really stuck on this all day today.  I am going to award you the points and I also increased it as well.  It is giving me though like 49.1877 and it should be 46.75 that comes up in excel and VB.  Any idea?  What I will be doing is this any help would be much appreciated. :).  I am going to have 4 values that I will have stored in variables.  Then I am going to try to write out in a row column the result.
0

Author Commented:
This is what I now have but I keep getting a type mismatch on IRRCalc.  I created a DLL to handle the function.

VB CLASS DLL:
Public Function IRRCalc(ByRef Values() As Double, ByRef Guess As Double) As String
IRRCalc = Format(IRR(Values(), Guess) * 100, "#0.00") & "%"
End Function

ASP PAGE:
<%@ Language=VBScript %>
<HTML>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<BODY>

<%
Dim objCalcVal, RetVal
Dim arrIRR(3),valGuess
arrIRR(0) = CDBL(-2465399)
arrIRR(1) = CDBL(1725956)
arrIRR(2) = CDBL(1686970)
arrIRR(3) = CDBL(1599299)
valGuess = 0.1
Set objCalcVal = Server.CreateObject("FinancialCalcs.clsFinancialCalcs")
RetVal = objCalcVal.IRRCalc(arrIRR,valGuess)
Response.Write RetVal
%>

</BODY>
</HTML>

0

Commented:
Every variable (even functions) in ASP/VBScript is effectively a variant.  VBScript will not accept a type declaration.  The type of a variable is automatically determined when the first value is assigned to it.  Format doesn't work either.  You will probably need to convert Values() to a string and do some manipulation  to truncate to 2 decimal points.  Round() or Trunc() might work, too.

Your IRRCalc function declaration then becomes:

Function IRRCalc(Values, Guess)
...

Since there is obviously much more that needs to be done to completely solve your problem I would recommend checking out the following sites.  They have been a great help to me.
http://www.w3schools.com/default.asp
http://www.learnasp.com/learnasp/
0

## Featured Post

• 2
• 2
Tackle projects and never again get stuck behind a technical roadblock.