简体   繁体   English

Excel function 自定义范围在 VBA

[英]Excel function with custom range in VBA

Question 1: In Excel, I want to use the XIRR formula on a discontinous range, eg I have dates in cells B3:F3 and cash flows in cells B6:F6.问题 1:在 Excel 中,我想在不连续范围内使用 XIRR 公式,例如,我在单元格 B3:F3 中有日期,在单元格 B6:F6 中有现金流量。 I need to put "-" before the first cash flow, but the formula below does not work = XIRR((-B6,C6:F6),B3:F3) So the only way I can think of is to copy all the cash flows and put - against the first one.我需要在第一笔现金流前加上“-”,但是下面的公式不起作用 = XIRR((-B6,C6:F6),B3:F3) 所以我能想到的唯一方法就是复制所有现金流动和放 - 反对第一个。 Can I use XIRR without having to copy the numbers so that the first cash flow is negative?我可以使用 XIRR 而不必复制数字,从而使第一笔现金流为负数吗? Please see请参见

Excel 屏幕截图

Question 2: I want to use goal-seek in VBA that loops through a series of data in rows.问题2:我想在VBA 中使用goal-seek 循环遍历一系列行中的数据。 Goal-seek can fail sometimes but if a reasonably close start value is provided then it is more likely to converge.目标寻找有时会失败,但如果提供了一个合理接近的起始值,那么它更有可能收敛。 This is the reason I use XIRR function to provide a close estimate.这就是我使用 XIRR function 提供接近估计的原因。 I have written a macro to do this but it copies formulae in Excel and then paste results, so it runs slow when data is large.我已经编写了一个宏来执行此操作,但它会复制 Excel 中的公式,然后粘贴结果,因此当数据很大时它运行缓慢。 How can I amend this macro so that all this operation is done in VBA (including calling Excel formula) and only the final results from goal-seek are pasted into cells A6:A8?如何修改这个宏,以便所有这些操作都在 VBA 中完成(包括调用 Excel 公式)并且只有目标搜索的最终结果被粘贴到单元格 A6:A8 中?

Sub Macro3()
'
' Macro3 Macro
'

'
    Range("B11").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=-R[-5]C"
    Range("B11").Select
    Selection.Copy
    Range("B11:B13").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("C11").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=R[-5]C"
    Range("C11").Select
    Selection.Copy
    Range("C11:F13").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("A11").Select
    ActiveCell.FormulaR1C1 = "=XIRR(RC[1]:RC[5],R3C2:R3C6)"
    Range("A11").Select
    Selection.Copy
    Range("A11:A13").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("G6").Select
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = _
        "=SUMPRODUCT(RC[-4]:RC[-1],(1+RC[-6])^(-R4C[-4]:R4C[-1]))-RC[-5]"
    Range("G6").Select
    Selection.Copy
    Range("G6:G8").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

For Row = 6 To 8
    Range("G" & Row).GoalSeek Goal:=0, ChangingCell:=Range("A" & Row)
Next Row


End Sub

If you have O365, you can calculate XIRR with the following formula:如果您有 O365,您可以使用以下公式计算XIRR

=XIRR(Returns*IF(SEQUENCE(,COLUMNS(Returns))=1,-1,1),Dates)

This will multiply the first Return by -1, and the others by 1.这会将第一个Return乘以 -1,其他的乘以 1。

IF you have an earlier version of Excel, without the SEQUENCE function, try:如果您有早期版本的 Excel,但没有SEQUENCE function,请尝试:

=XIRR(Returns*IF(COLUMN(INDEX($1:$1,1,1):INDEX($1:$1,1,COUNT(Returns)))=1,-1,1),Dates)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM