我正在尝试创建一个计算Drawdown的函数。 它的工作方式如下:
在这种情况下,最大的缩进出现在黄色区域!也就是说,公式如下所示:Drawdown =(MaxValue / Value)-1 ==> Drawdown =(13/9)-1
我尝试如下,但没有结果:
Public Function MDD(ByVal Selection0, ByVal Selection1)
'Function Max DrawDown
Dim i As Long
Dim Drawdown0 As Long
Dim Drawdown1 As Long
i = 2
Drawdown0 = "(" & Selection0 & "/MAX(" & Selection1 & ")) - 1"
While i < Plan1.Range("B" & Rows.Count).End(xlUp).Row + 1
Drawdown1 = "(" & Selection0 & "/MAX(" & Selection1 & ")) - 1"
If Drawdown1 > Drawdown0 Then
Drawdown0 = Drawdown1
End If
i = i + 1
Wend
MDD = Drawdown0
End Function
Sub lsMDD()
Application.MacroOptions Macro:="MDD", Category:=4
End Sub
哪里出错了?
答案 0 :(得分:3)
您不需要遍历整个范围。查看应用程序。WorksheetFunction-满足您的所有需求。
Public Function MDD(ByVal pRange As Variant) As Variant
MDD = Application.WorksheetFunction.Max(pRange) / Application.WorksheetFunction.Min(pRange) - 1
End Function