[英]Cycle through a list of Investors and calculate the XIRR for each one to automate process
So I have been stuck on this problem for a few days.所以我已经被这个问题困扰了几天。 I have looked at some others codes but I am still coming up short.
我已经查看了其他一些代码,但我仍然不足。 I am not the best at VBA either.
我在 VBA 方面也不是最好的。
I have a list of investors with their attached payments and dates.我有一份投资者名单,附有他们的付款和日期。 I am trying to run a command button that will go through each Account, find their related payments and dates, run the XIRR function and then place the XIRR value at the bottom to the right of each account.
我正在尝试运行一个命令按钮,该按钮将通过每个帐户 go,找到它们的相关付款和日期,运行 XIRR function,然后将 XIRR 值放在每个帐户右侧的底部。 This is simple enough to do by hand but when you have a spreadsheet of 15000 cells+ it becomes tedious and I am trying to automate this process.
这很简单,可以手工完成,但是当你有一个包含 15000 个单元格的电子表格时,它会变得乏味,我正在尝试自动化这个过程。 It becomes difficult because each investor has different payment amounts so to find the correct location to place the XIRR value has also stumped me.
这变得很困难,因为每个投资者都有不同的付款金额,所以找到放置 XIRR 值的正确位置也难倒了我。
Here is an example of my spreadsheet这是我的电子表格的示例
Dim i As Integer
Dim x As Double
Dim dateArray() As Date
Dim dateStrings() As String
Dim valArray() As Double
ReDim dateArray(Dates.Count)
ReDim valArray(Trans.Count)
ReDim dateStrings(Dates.Count)
'Sheets("InvestorList").PivotTables.GetPivotData("Account", "x") = i
'Sheets("AccountPayments").Find ("i")
End Sub
Public Function MyXIRR(Dates As Range, Trans As Range, Balance As Double)
For i = 1 To Dates.Count
dateArray(i - 1) = Dates.Item(i).Value
Next i
For i = 1 To Trans.Count
valArray(i - 1) = Trans.Item(i).Value
Next i
'Set the date on the "Balance" line to one day after the last transaction date
dateArray(Dates.Count) = DateAdd("d", 1, Dates.Item(Dates.Count))
valArray(Trans.Count) = -1 * Balance
For i = 0 To Dates.Count
dateStrings(i) = Format(dateArray(i), "mm/dd/yyyy")
Next i
MyXIRR = Application.WorksheetFunction.Xirr(valArray, dateStrings)
End Function
So I counseled with a college and he helped reduce my code to something much simpler and cleaner.所以我咨询了一所大学,他帮助我将代码简化为更简单、更干净的代码。 I ran this code with data and it worked great.
我用数据运行了这段代码,效果很好。 Some spot checking may be needed if an XIRR value doesn't appear right but this helps automate the process.
如果 XIRR 值显示不正确,则可能需要进行一些抽查,但这有助于使过程自动化。
Private Sub CommandButton1_Click()
Dim myrow As Integer
Dim startrow As Integer
Dim valuerange As String
Dim daterange As String
Dim investor As String
myrow = 2
startrow = 2
investor = Cells(myrow, 1)
Do Until Cells(myrow, 1) = ""
If Cells(myrow + 1, 1) <> investor Then
'We are at the end of the list for the current investor.
daterange = "R" & startrow & "C2:R" & myrow & "C2"
valuerange = "R" & startrow & "C3:R" & myrow & "C3"
Cells(myrow, 4) = "=XIRR(" & valuerange & ", " & daterange & ")"
startrow = myrow + 1
investor = Cells(myrow + 1, 1)
End If
myrow = myrow + 1
Loop
End Sub
I would recommend trying the macro recorder.我建议尝试使用宏记录器。 There are many times I have gotten frustrated and tried searching all over for solutions when I could just record my steps... If you are unsure how to do so, here are the steps!
有很多次,当我可以记录我的步骤时,我感到沮丧并尝试到处寻找解决方案......如果你不确定如何做到这一点,这里是步骤!
In excel:在 excel 中:
Hope this helps: Happy coding :)希望这会有所帮助:快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.