Solved

# Need IRR Function similar to Excel and VB for ASP

Posted on 2005-05-13
905 Views
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
Question by:sbornstein2

LVL 9

Accepted Solution

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 Comment

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 Comment

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

LVL 9

Expert Comment

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

### Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
how to add IIS SMTP to handle application/Scanner relays into office 365.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…