J
James
For the past month I've been working on a solution for the calculation of
internal rate of return in Access with VBA with the help of a few of you
(thanks!). Everything seems to calculate fine but only under certain
conditions. For instance, if you use the IRR function in Access your term
can not be more than 24 months or it simply won't return an answer. For
accessing the Excel function from Access, you can't go past 48 months or it
too won't return a result. I originally setup two boxes on a form to show me
the results side by side for both calculations and when they did return an
answer they both return the proper answers.
So here is the code. I've placed some test information at the top of the
VBA code for you to experiment with and see if you can observe the same
issue(s). I've also included what the correct answer should be as
calculated directly from Excel 2007. Normally, there would be variables
intermingled in the code instead of just numbers as indicated below. The
Access calculation is at the top and the near duplicate Excel code with late
binding is towards the bottom.
I'd GREATLY appreciate any comments as if this is a Microsoft issue or
something else. (Environment is Vista64, Excel/Access 2007 with latest
service packs).
Thank you very much for your assistance!!
'When testing for 24 months use IRRValuex(0) = -18468.83, IRRCashFlowx =
957.55, IRRFinalCashFlow = 100, Termx = 24
'=============>>Correct IRR for 24 months should be: 18.64%
'When testing for 36 months use IRRValuex(0) = -18917.23,
IRRCashFlowx = 657.55, IRRFinalCashFlow = 100, Termx = 36
'=============>>Correct IRR for 36 months should be: 13.83%
'When testing for 42 months use IRRValuex(0) = -19044.14,
IRRCashFlowx = 572.64, IRRFinalCashFlow = 100, Termx = 42
'=============>>Correct IRR for 42 months should be: 12.66%
'When testing for 48 months use IRRValuex(0) = -19139.93,
IRRCashFlowx = 508.49, IRRFinalCashFlow = 100, Termx = 48
'=============>>Correct IRR for 48 months should be: 11.76%
'When testing for 54 months use IRRValuex(0) = -19214.71,
IRRCashFlowx = 458.49, IRRFinalCashFlow = 100, Termx = 54
'=============>>Correct IRR for 54 months should be: 11.06%
'When testing for 60 months use IRRValuex(0) = -19273.93,
IRRCashFlowx = 418.87, IRRFinalCashFlow = 100, Termx = 60
'=============>>Correct IRR for 60 months should be: 10.56%
'When testing for 72 months use IRRValuex(0) = -19362.77,
IRRCashFlowx = 359.43, IRRFinalCashFlow = 100, Termx = 72
'=============>>Correct IRR for 72 months should be: 9.80%
'===========================================================================
'Access IRR Calculation
Dim GuessA, FmtA, IRRFinalCashFlowA, CountA, IRRCashFlowA, TermA
Dim IngSizeA As Long
Dim IRRValuesA() As Double ' Set up array
TermA = 24 ' Set term of loan -- Normally this would be a variable
from a form--used for testing for next row
IngSizeA = TermA + 1 ' Set array size
ReDim IRRValuesA(IngSizeA)
IRRCashFlowA = 957.55
IRRFinalCashFlowA = 100
GuessA = 0.125 ' Guess starts at 12.5 percent
FmtA = "#0.00" ' Define percentage format
CountA = 0 ' Zero out the count amount for the cashflow
assignment loop
'IRRValuesA(0) is the first value of the array and is always a
negative value
'IRRCashFlowA are all of the cashflows from 1 to one minus the term
of the loan -- if the term of the
' loan is 48 then it would be the values from 1 to
47
'IRRFinalCashFlowA is the last cashflow of the array which is always
going to be a sum of other variables
' that is less than a normal cashflow
IRRValuesA(0) = -18468.83
Do While CountA <= (TermA - 2) ' compensates for starting at 0
instead of 1
CountA = CountA + 1
IRRValuesA(CountA) = IRRCashFlowA
Loop
IRRValuesA(TermA - 1) = IRRFinalCashFlowA ' compensates for starting
at 0 instead of 1
'Have Access 2007 Calculate the Internal Rate of Return (IRR)
Me.txtACCESSIRR = IRR(IRRValuesA(), GuessA) * 12
'===========================================================================
'Excel IRR Calculation
Dim GuessE, FmtE, IRRFinalCashFlowE, CountE, IRRCashFlowE, TermE
Dim IngSizeE As Long
Dim IRRValuesE() As Variant ' Set up array.
TermE = 24 ' Set term of loan -- Normally this would be a variable
from a form--used for testing for next row
IngSizeE = TermE + 1 ' Set array size.
ReDim IRRValuesE(IngSizeE)
IRRCashFlowE = 957.55
IRRFinalCashFlowE = 100
GuessE = 0.125 ' Guess starts at 12.5 percent.
FmtE = "#0.00" ' Define percentage format.
CountE = 0 ' Zero out the count amount.
'IRRValuesE(0) is the first value of the array and is always a
negative value
'IRRCashFlowE are all of the cashflows from 1 to one minus the term
of the loan -- if the term of the
' loan is 48 then it would be the values from 1 to
47
'IRRFinalCashFlowE is the last cashflow of the array which is always
going to be a sum of other variables
' that is less than a normal cashflow
IRRValuesE(0) = -18468.83
Do While CountE <= (TermE - 2) ' compensates for starting at 0
instead of 1
CountE = CountE + 1
IRRValuesE(CountE) = IRRCashFlowE
Loop
IRRValuesE(TermE - 1) = IRRFinalCashFlowE ' compensates for starting
at 0 instead of 1
'Have Excel 2007 Calculate the Internal Rate of Return (IRR) with
late binding so the Excel version doesn't matter
Dim IRRref As Reference
'Late Binding
Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
On Error Resume Next
Set IRRref = References!Excel
If Err.Number = 0 Then
References.Remove IRRref
ElseIf Err.Number <> 9 _
Then 'Subscript out of range meaning not reference not
found
MsgBox Err.Description
Exit Sub
End If
Me.txtEXCELIRR = objXL.IRR(IRRValuesE(), GuessE) * 12
objXL.Quit
Set objXL = Nothing
internal rate of return in Access with VBA with the help of a few of you
(thanks!). Everything seems to calculate fine but only under certain
conditions. For instance, if you use the IRR function in Access your term
can not be more than 24 months or it simply won't return an answer. For
accessing the Excel function from Access, you can't go past 48 months or it
too won't return a result. I originally setup two boxes on a form to show me
the results side by side for both calculations and when they did return an
answer they both return the proper answers.
So here is the code. I've placed some test information at the top of the
VBA code for you to experiment with and see if you can observe the same
issue(s). I've also included what the correct answer should be as
calculated directly from Excel 2007. Normally, there would be variables
intermingled in the code instead of just numbers as indicated below. The
Access calculation is at the top and the near duplicate Excel code with late
binding is towards the bottom.
I'd GREATLY appreciate any comments as if this is a Microsoft issue or
something else. (Environment is Vista64, Excel/Access 2007 with latest
service packs).
Thank you very much for your assistance!!
'When testing for 24 months use IRRValuex(0) = -18468.83, IRRCashFlowx =
957.55, IRRFinalCashFlow = 100, Termx = 24
'=============>>Correct IRR for 24 months should be: 18.64%
'When testing for 36 months use IRRValuex(0) = -18917.23,
IRRCashFlowx = 657.55, IRRFinalCashFlow = 100, Termx = 36
'=============>>Correct IRR for 36 months should be: 13.83%
'When testing for 42 months use IRRValuex(0) = -19044.14,
IRRCashFlowx = 572.64, IRRFinalCashFlow = 100, Termx = 42
'=============>>Correct IRR for 42 months should be: 12.66%
'When testing for 48 months use IRRValuex(0) = -19139.93,
IRRCashFlowx = 508.49, IRRFinalCashFlow = 100, Termx = 48
'=============>>Correct IRR for 48 months should be: 11.76%
'When testing for 54 months use IRRValuex(0) = -19214.71,
IRRCashFlowx = 458.49, IRRFinalCashFlow = 100, Termx = 54
'=============>>Correct IRR for 54 months should be: 11.06%
'When testing for 60 months use IRRValuex(0) = -19273.93,
IRRCashFlowx = 418.87, IRRFinalCashFlow = 100, Termx = 60
'=============>>Correct IRR for 60 months should be: 10.56%
'When testing for 72 months use IRRValuex(0) = -19362.77,
IRRCashFlowx = 359.43, IRRFinalCashFlow = 100, Termx = 72
'=============>>Correct IRR for 72 months should be: 9.80%
'===========================================================================
'Access IRR Calculation
Dim GuessA, FmtA, IRRFinalCashFlowA, CountA, IRRCashFlowA, TermA
Dim IngSizeA As Long
Dim IRRValuesA() As Double ' Set up array
TermA = 24 ' Set term of loan -- Normally this would be a variable
from a form--used for testing for next row
IngSizeA = TermA + 1 ' Set array size
ReDim IRRValuesA(IngSizeA)
IRRCashFlowA = 957.55
IRRFinalCashFlowA = 100
GuessA = 0.125 ' Guess starts at 12.5 percent
FmtA = "#0.00" ' Define percentage format
CountA = 0 ' Zero out the count amount for the cashflow
assignment loop
'IRRValuesA(0) is the first value of the array and is always a
negative value
'IRRCashFlowA are all of the cashflows from 1 to one minus the term
of the loan -- if the term of the
' loan is 48 then it would be the values from 1 to
47
'IRRFinalCashFlowA is the last cashflow of the array which is always
going to be a sum of other variables
' that is less than a normal cashflow
IRRValuesA(0) = -18468.83
Do While CountA <= (TermA - 2) ' compensates for starting at 0
instead of 1
CountA = CountA + 1
IRRValuesA(CountA) = IRRCashFlowA
Loop
IRRValuesA(TermA - 1) = IRRFinalCashFlowA ' compensates for starting
at 0 instead of 1
'Have Access 2007 Calculate the Internal Rate of Return (IRR)
Me.txtACCESSIRR = IRR(IRRValuesA(), GuessA) * 12
'===========================================================================
'Excel IRR Calculation
Dim GuessE, FmtE, IRRFinalCashFlowE, CountE, IRRCashFlowE, TermE
Dim IngSizeE As Long
Dim IRRValuesE() As Variant ' Set up array.
TermE = 24 ' Set term of loan -- Normally this would be a variable
from a form--used for testing for next row
IngSizeE = TermE + 1 ' Set array size.
ReDim IRRValuesE(IngSizeE)
IRRCashFlowE = 957.55
IRRFinalCashFlowE = 100
GuessE = 0.125 ' Guess starts at 12.5 percent.
FmtE = "#0.00" ' Define percentage format.
CountE = 0 ' Zero out the count amount.
'IRRValuesE(0) is the first value of the array and is always a
negative value
'IRRCashFlowE are all of the cashflows from 1 to one minus the term
of the loan -- if the term of the
' loan is 48 then it would be the values from 1 to
47
'IRRFinalCashFlowE is the last cashflow of the array which is always
going to be a sum of other variables
' that is less than a normal cashflow
IRRValuesE(0) = -18468.83
Do While CountE <= (TermE - 2) ' compensates for starting at 0
instead of 1
CountE = CountE + 1
IRRValuesE(CountE) = IRRCashFlowE
Loop
IRRValuesE(TermE - 1) = IRRFinalCashFlowE ' compensates for starting
at 0 instead of 1
'Have Excel 2007 Calculate the Internal Rate of Return (IRR) with
late binding so the Excel version doesn't matter
Dim IRRref As Reference
'Late Binding
Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
On Error Resume Next
Set IRRref = References!Excel
If Err.Number = 0 Then
References.Remove IRRref
ElseIf Err.Number <> 9 _
Then 'Subscript out of range meaning not reference not
found
MsgBox Err.Description
Exit Sub
End If
Me.txtEXCELIRR = objXL.IRR(IRRValuesE(), GuessE) * 12
objXL.Quit
Set objXL = Nothing