I currently have a query XIRR_Array, that returns three fields: Match Code (a concatenation of investor name and product code), CFlow, which is the cash flow (negative for outflow, positive for inflow) and TDate, which is the date of the cash flow.
I have been trying to derive the XIRR for each match code using my less-than-rudimentary VBA skills.
An extract of data from XIRR_Array (In the form of an eponymous table), the query I have used to get the XIRR and the VBA code I used in Module1 to define the XIRR function are all in the attached database.
As can be seen in the XIRR_Array table, the query has already sorted the data, and also supplied a month-end value (TDate=31/08/2016) for the XIRR evaluation.
The query I have used:
The VBA code for defining AXIRR:
On running this, I get the following error: ‘run time error 424 object required’, and the debug highlights the line: ReDim CFlow(rs.RecordCount - 1)
Can someone guide me on this?
Sanjay
I have been trying to derive the XIRR for each match code using my less-than-rudimentary VBA skills.
An extract of data from XIRR_Array (In the form of an eponymous table), the query I have used to get the XIRR and the VBA code I used in Module1 to define the XIRR function are all in the attached database.
As can be seen in the XIRR_Array table, the query has already sorted the data, and also supplied a month-end value (TDate=31/08/2016) for the XIRR evaluation.
The query I have used:
Code:
SELECT DISTINCT [Match Code] FROM XIRR_ARRAY, AXIRR([Match Code],0.1) AS CAGR
The VBA code for defining AXIRR:
Code:
Option Compare Database
Function AXIRR(MatchCode As String, Optional GuessRate As Double = 0.1) As Variant
Dim rsXIRR As dao.Recordset
Dim db As Database
Dim CFlow() As Currency
Dim TDate() As Date
Dim SelectSql As String
Dim I As Integer
SelectSql = "SELECT CFlow, TDate FROM XIRR_Array WHERE [Match Code]= 'MatchCode' ORDER BY TDate"
'Debug.Print strSql
Set db = CurrentDb
Set rsXIRR = db.OpenRecordset(SelectSql, dbOpenDynaset)
'Fill Payments and dates.
ReDim CFlow(rs.RecordCount - 1)
ReDim TDate(rs.RecordCount - 1)
Do While Not rs.EOF
CFlow(I) = rs.Fields(CFlow).Value
TDate(I) = rs.Fields(TDate).Value
Debug.Print I
I = I + 1
rs.MoveNext
Loop
For I = 0 To rs.RecordCount - 1
Debug.Print CFlow(I) & " " & TDate(I)
Next I
AccessXIRR = XIRR_Wrapper(CFlow, TDate, GuessRate)
End Function
Public Function XIRR_Wrapper(Payments() As Currency, Dates() As Date, Optional GuessRate As Double = 0.1)
'Must add a reference to the Xcel library. Use Tools, References, Microsoft Excel XX.X Object Library
XIRR_Wrapper = Excel.WorksheetFunction.Xirr(Payments, Dates, GuessRate)
End Function
On running this, I get the following error: ‘run time error 424 object required’, and the debug highlights the line: ReDim CFlow(rs.RecordCount - 1)
Can someone guide me on this?
Sanjay