[英]Excel VBA routine to UDF
我正在為我的金融課程做練習,其中我在Excel VBA中編寫了一個例程,該例程可以按預期工作,而無需使用MMULT或TRANSPOSE。 我想通過將該例程改編為UDF來在Excel Sheet中實現結果,但是以某種方式我只遇到了一個值錯誤。
我有點失落……有什么提示嗎?
這是例行程序:
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
您需要設置斷點,並查看#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傳遞,則在執行其余的UDF之前,需要將函數參數的類型設為Variant,並進行測試以查看其含義並進行適當的轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.