Excel VBA例程到UDF

时间:2016-09-05 13:12:57

标签: excel vba

我正在为我的财务课做练习,我已经在Excel VBA中编写了一个按预期工作的例程,而不使用MMULT或TRANSPOSE。我希望通过将例程调整为UDF来实现Excel表格中的结果,但不知怎的,我只是得到了一个值错误。

我有点遗失某些东西......有什么提示吗?

这里的例程:

Option Explicit
Public Sub CalcVola2()

Dim WeightedVola As Variant, Weights As Variant, Volatilities As Variant, Correlations As Variant
Dim i As Double, j As Double, CorrSum As Double, VarSum As Double
Dim CalcVola2 As Double


'===================================================================================================
' Load data
'===================================================================================================

Weights = ThisWorkbook.Worksheets("Stetig").Range("FR4:FR43")
Volatilities = ThisWorkbook.Worksheets("Stetig").Range("FS4:FS43")

Correlations = ThisWorkbook.Worksheets("Covar-Correl").Range("C13:AP52")

'===================================================================================================
' Resize weighted volatility array to fit the inputs and clean the data
'===================================================================================================

ReDim WeightedVola(1 To UBound(Weights, 1), 1 To 1)

For i = 1 To UBound(Weights, 1)
    If Weights(i, 1) = "" Then
        Weights(i, 1) = 0
    End If
Next i

For i = 1 To UBound(Volatilities, 1)
    If Volatilities(i, 1) = "" Then
        Volatilities(i, 1) = 0
    End If
Next i

'===================================================================================================
' Perform weighted vola calculations
'===================================================================================================

For i = 1 To UBound(Weights, 1)
   WeightedVola(i, 1) = Weights(i, 1) * Volatilities(i, 1)
Next i


'===================================================================================================
' Calculate the first sum of the portfolio volatility function by adding the squared weighted volas
'===================================================================================================

For i = 1 To UBound(Weights, 1)
    CorrSum = CorrSum + WeightedVola(i, 1) ^ 2
Next i


'===================================================================================================
' Calculate the second sum of the portfolio volatility function by the product of the weighted vola
' and the correlation
'===================================================================================================

For i = 1 To UBound(Weights, 1)
    For j = i + 1 To UBound(Weights, 1)
        CorrSum = CorrSum + WeightedVola(i, 1) * 2 * WeightedVola(j, 1) * Correlations(i, j)
    Next j
Next i

CalcVola2 = Sqr(CorrSum)

ThisWorkbook.Worksheets("Stetig").Range("FS46").Value = CorrSum
ThisWorkbook.Worksheets("Stetig").Range("FS47").Value = CalcVola2


End Sub

这里是UDF:

Option Explicit
Public Function CalcVola(Weights As Variant, Volatilities As Variant, Correlations As Variant) As Double

Dim WeightedVola As Variant
Dim i As Double, j As Double, CorrSum As Double, VarSum As Double


'===================================================================================================
' Resize weighted volatility array to fit the inputs and clean the data
'===================================================================================================

ReDim WeightedVola(1 To UBound(Weights, 1), 1 To 1)

For i = 1 To UBound(Weights, 1)
    If Weights(i, 1) = "" Then
        Weights(i, 1) = 0
    End If
Next i

For i = 1 To UBound(Volatilities, 1)
    If Volatilities(i, 1) = "" Then
        Volatilities(i, 1) = 0
    End If
Next i


'===================================================================================================
' Perform weighted vola calculations
'===================================================================================================

For i = 1 To UBound(Weights, 1)
   WeightedVola(i, 1) = Weights(i, 1) * Volatilities(i, 1)
Next i


'===================================================================================================
' Calculate the first sum of the portfolio volatility function by adding the squared weighted volas
'===================================================================================================

For i = 1 To UBound(Weights, 1)
    CorrSum = CorrSum + WeightedVola(i, 1) ^ 2
Next i


'===================================================================================================
' Calculate the second sum of the portfolio volatility function by the product of the weighted vola
' and the correlation
'===================================================================================================

For i = 1 To UBound(Weights, 1)
    For j = i + 1 To UBound(Weights, 1)
        CorrSum = CorrSum + WeightedVola(i, 1) * 2 * WeightedVola(j, 1) * Correlations(i, j)
    Next j
Next i

CalcVola = Sqr(CorrSum)


End Function

1 个答案:

答案 0 :(得分:1)

您需要设置断点并查看返回#VALUE!错误的位置。通常是因为变量未正确输入,或者VBA函数获取的参数不正确。例如,如果您将Weights参数作为一维数组传递给函数,那么您的例程将崩溃并在第一个#VALUE!行返回Redim错误,因为它正在查找用于2D阵列。

如果您的参数作为范围传递,则会出现类似的问题。

如果情况总是这样,请将参数作为范围传递,然后在代码中传递:

Public Function CalcVola(rWeights As Range, rVolatilities As Range, rCorrelations As Range) As Double

Dim Weights, Volatilities, Correlations

Weights = rWeights
Volatilities = rVolatilities
Correlations = rCorrelations

...

End Function

如果参数可以作为Ranges或Arrays传递,那么你需要将你的函数参数作为Variant类型,并测试它们是什么,并在执行其余的之前进行适当的转换。你的UDF。