My user-defined formula as shown below is working properly in a Function
environment as long as this is embedded into a module within the workbook I am primarily using it. However, when I start to put it into an .xlam
(AddIn) it is not working anymore. In the code below I indicated the line where it starts to crash. I suspect it is related to either .ThisCell
or .Find
, but was not able to isolate the issue. Any ideas?
Public Function zinsfuss(cashflows, dates) As Variant
'-----------------------------------------------------------------
Application.Calculation = xlCalculationAutomatic
Application.Volatile True
'-----------------------------------------------------------------
Dim arDates() As Variant
If dates.Rows.Count > 1 Then
arDates = Application.Transpose(dates)
Else:
ReDim arDates(1 To 1)
arDates(1) = CDbl(dates)
End If
ReDim Preserve arDates(1 To UBound(arDates) + 1)
arDates(UBound(arDates)) = CDbl(Date)
'-----------------------------------------------------------------
Dim arCF() As Variant
If dates.Rows.Count > 1 Then
arCF = Application.Transpose(cashflows)
Else:
ReDim arCF(1 To 1)
arCF(1) = cashflows
End If
ReDim Preserve arCF(1 To UBound(arCF) + 1)
'starting with the next line the code stumbles.
arCF(UBound(arCF)) = ThisWorkbook.Sheets(Application.ThisCell.Parent.Name).Cells.Find("Kurswert in Fondswährung", LookIn:=xlValues, LookAt:=xlWhole).Offset(0, 1).Value
'-----------------------------------------------------------------
zinsfuss = WorksheetFunction.Xirr(arCF, arDates)
'-----------------------------------------------------------------
End Function