因此,在经过一些冗长的调试之后,我追踪了一个来自我最后一个For循环结尾的难以理解的错误。首先,这是我的代码,
Function constant_covariance(inputs)
Dim c_matrix() As Variant
ReDim c_matrix(inputs.Rows.Count, inputs.Rows.Count)
Rs = inputs.Rows.Count
Cols = inputs.Columns.Count
If (Rs <> Cols) Then 'Error if dimensions of range for covariance matrix not equal.
MsgBox "Dimensions of range not equal; try again."
Exit Function
End If
Sum = 0
For i = 1 To Rs 'This is to create my average correlation value.
range1 = Range(inputs(1, i), inputs(1, i).End(xlDown))
For j = 1 To Cols
range2 = Range(inputs(1, j), inputs(1, j).End(xlDown))
If Not (i = j) Then
Sum = Sum + WorksheetFunction.Covar(range1, range2)
End If
Next j
Next i
avg_corr = Sum / Rs 'Average correlation value.
For i = 1 To Rs 'Regular correlation matrix, with only modification in last line before "End If".
range1 = Range(inputs(1, i), inputs(1, i).End(xlDown))
For j = 1 To Cols
range2 = Range(inputs(1, j), inputs(1, j).End(xlDown))
If (i = j) Then
c_matrix(i, j) = WorksheetFunction.Var(range1)
ElseIf Not (i = j) Then
c_matrix(i, j) = (WorksheetFunction.Covar(range1, range2)) ^ 0.5 * avg_corr
End If
Next j
Next i
constant_covariance = (WorksheetFunction.Covar(range1, range2)) ^ 0.5 * avg_corr
End Function
在最底部,当我获取两个输入区域的协方差并将其提高到0.5的幂,然后将其乘以另一个变量时,我得到VALUE
错误。事情是,我把与我的函数输出完全相同的代码,仅用于测试目的,并且在那里工作正常。
我的数组是否有问题,它不喜欢过于复杂的操作是其成员之一的输入?有人请解释一下我的错误来源是什么吗?