2016-09-05 3 views
0

저는 Excel VBA에서 MMULT 또는 TRANSPOSE를 사용하지 않고 의도 한대로 작동하는 루틴을 작성한 금융 클래스의 연습을하고 있습니다. 루틴을 UDF로 적용하여 Excel 시트 내에서 결과를 구현하려고했지만 어떻게 든 가치 오류가 발생했습니다.UDF로 Excel VBA 루틴

나는 뭔가를 놓치고 있습니다 ... 어떤 힌트라도? 여기

루틴 :

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를 : 당신은 중단 점을 설정하고 #VALUE! 오류가 반환됩니다 위치를 확인할 필요가

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 
+0

'# VALUE' 오류를주는 예제 수식이 UDF와 어떤 관계가 있습니까? – Comintern

+2

중단 점을 설정하고'#VALUE! '오류가 반환되는 위치를 확인해야합니다. 변수가 올바르게 입력되지 않았거나 VBA 함수가 잘못된 인수를 가져 오기 때문에 자주 발생합니다. 예를 들어, 함수에'Weights' 인수를 1 차원 배열로 전달하면, 루틴은 충돌하여 첫 번째'Redim' 행에'#VALUE! '오류를 반환합니다. 왜냐하면 2D 배열을 찾고 있기 때문입니다 . –

+0

@Comintern 수식은 실제로 상관 관계를 사용하여 포트폴리오 변동성입니다. http://d3plit93wdq7ew.cloudfront.net/assets/covar-300x105.png –

답변

1

. 변수가 올바르게 입력되지 않았거나 VBA 함수가 잘못된 인수를 가져 오기 때문에 자주 발생합니다. 예를 들어 Weights 인수를 함수에 1 차원 배열로 전달하면 루틴이 충돌하여 해당 행의 첫 번째 문자 Redim#VALUE! 오류가 반환됩니다 (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 

, 당신은거야 Variant 유형으로 함수 인수를 가져와 나머지 UDF를 실행하기 전에 해당 함수가 무엇인지 확인하고 적절한 변환을 수행해야합니다.