I am getting this error in VBA, when I try to get the correlation coefficient of two arrays Arr1 and Arr2
Dim arr1() As Variant
Dim arr2() As Variant
arr1 and arr2 are initialized in the function.
Dim result As Double
result = Application.WorksheetFunction.Correl(arr1, arr2)
The Correl
method throws an exception
"unable to get correl property of the worksheetfunction class"
Any idea what could be the reason for the exception.
EDIT :
I think this problem occurs when one of the array has all the elements same. In this case, the correlation coefficient results in #DIV0 error.
Thanks
Like in many worksheet functions, arguments for
Application.WorksheetFunction.Correl
must have Range, NOT Variant type.Additionally in
Correl
function standard deviation of any of these 2 ranges can't be 0.Empty values instead of zeros can also sometimes cause this error.
Not following these rules, results in exactly the same error OP experienced.
I think you have forgotten to initialize your arrays.
This pretty simple example works:
Kiran,
Two possible solutions that prevent this error ... but seeing your data would be much more helpful