UDF Error - Structure or Datatype?

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
I am trying to generate Pearson's Correlation Matrix; and subsequently Spearman's.

When I use the Excel's in-built Correl function, I get the output. But, when I use my custom function for correl, I get a VALUE# error. That is, if I replace "Application.WorksheetFunction.Correl" with "PEARSONRHO" I do not get an output.

Functions are:
Code:
Public Function PEARSONMAT(ByVal rng As Range) As Variant

    Dim outRslt() As Double, i As Long, j As Long, iCols As Long


    iCols = rng.Columns.Count
    ReDim outRslt(iCols - 1, iCols - 1)


    For i = 1 To iCols
        For j = 1 To iCols
            outRslt(i - 1, j - 1) = Application.WorksheetFunction.Correl(rng.Columns(i), rng.Columns(j))
        Next j
    Next i


    PEARSONMAT = outRslt


End Function

Public Function PEARSONRHO(ByVal x As Variant, ByVal y As Variant) As Variant


    Dim sx As Double, sy As Double
    Dim s1 As Double, s2 As Double, s3 As Double
    Dim k As Long


    sx = 0
    sy = 0
    s1 = 0
    s2 = 0
    s3 = 0


    For k = 1 To x.Rows.Count
        sx = x(k) + sx
        sy = y(k) + sy
    Next


    sx = sx / x.Rows.Count
    sy = sy / y.Rows.Count


    For k = 1 To x.Rows.Count
        s1 = s1 + (x(k) - sx) * (y(k) - sy)
        s2 = s2 + (x(k) - sx) ^ 2
        s3 = s3 + (y(k) - sy) ^ 2
    Next


    PEARSONRHO = s1 / Sqr(s2 * s3)


End Function
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Application.WorksheetFunction is only for built-in Excel worksheet functions.
 
Upvote 0
Yes, I am aware of the same. When I replace the whole of "Application.WorksheetFunction.Correl" with PEARSONRHO I get an error. PEARSONRHO on a standalone basis works with x,y data.
 
Upvote 0
So where/when do you get the error?
 
Upvote 0
Let me explain, I have the following data:

[TABLE="width: 256"]
<colgroup><col span="4"></colgroup><tbody>[TR]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD]d[/TD]
[/TR]
[TR]
[TD="align: right"]44.00[/TD]
[TD="align: right"]89.47[/TD]
[TD="align: right"]44.61[/TD]
[TD="align: right"]11.37[/TD]
[/TR]
[TR]
[TD="align: right"]44.00[/TD]
[TD="align: right"]85.84[/TD]
[TD="align: right"]54.30[/TD]
[TD="align: right"]8.65[/TD]
[/TR]
[TR]
[TD="align: right"]38.00[/TD]
[TD="align: right"]89.02[/TD]
[TD="align: right"]49.87[/TD]
[TD="align: right"]11.95[/TD]
[/TR]
[TR]
[TD="align: right"]40.00[/TD]
[TD="align: right"]75.98[/TD]
[TD="align: right"]45.68[/TD]
[TD="align: right"]11.95[/TD]
[/TR]
[TR]
[TD="align: right"]44.00[/TD]
[TD="align: right"]81.42[/TD]
[TD="align: right"]39.44[/TD]
[TD="align: right"]13.08[/TD]
[/TR]
[TR]
[TD="align: right"]44.00[/TD]
[TD="align: right"]73.03[/TD]
[TD="align: right"]50.54[/TD]
[TD="align: right"]10.13[/TD]
[/TR]
[TR]
[TD="align: right"]45.00[/TD]
[TD="align: right"]66.45[/TD]
[TD="align: right"]44.75[/TD]
[TD="align: right"]11.12[/TD]
[/TR]
[TR]
[TD="align: right"]54.00[/TD]
[TD="align: right"]83.12[/TD]
[TD="align: right"]51.86[/TD]
[TD="align: right"]10.33[/TD]
[/TR]
[TR]
[TD="align: right"]51.00[/TD]
[TD="align: right"]69.63[/TD]
[TD="align: right"]40.84[/TD]
[TD="align: right"]10.95[/TD]
[/TR]
[TR]
[TD="align: right"]48.00[/TD]
[TD="align: right"]91.63[/TD]
[TD="align: right"]46.77[/TD]
[TD="align: right"]10.25[/TD]
[/TR]
[TR]
[TD="align: right"]57.00[/TD]
[TD="align: right"]73.37[/TD]
[TD="align: right"]39.41[/TD]
[TD="align: right"]12.63[/TD]
[/TR]
[TR]
[TD="align: right"]52.00[/TD]
[TD="align: right"]76.32[/TD]
[TD="align: right"]45.44[/TD]
[TD="align: right"]9.63[/TD]
[/TR]
[TR]
[TD="align: right"]51.00[/TD]
[TD="align: right"]67.25[/TD]
[TD="align: right"]45.12[/TD]
[TD="align: right"]11.08[/TD]
[/TR]
[TR]
[TD="align: right"]51.00[/TD]
[TD="align: right"]73.71[/TD]
[TD="align: right"]45.79[/TD]
[TD="align: right"]10.47[/TD]
[/TR]
</tbody>[/TABLE]

With "Application.WorksheetFunction.Correl" in PEARSONMAT I get the following output from PEARSONMAT as an Array formula:

[TABLE="width: 256"]
<colgroup><col span="4"></colgroup><tbody>[TR]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD]d[/TD]
[/TR]
[TR]
[TD="align: right"]1.000000[/TD]
[TD="align: right"]-0.357957[/TD]
[TD="align: right"]-0.305628[/TD]
[TD="align: right"]-0.129392[/TD]
[/TR]
[TR]
[TD="align: right"]-0.357957[/TD]
[TD="align: right"]1.000000[/TD]
[TD="align: right"]0.383502[/TD]
[TD="align: right"]-0.095889[/TD]
[/TR]
[TR]
[TD="align: right"]-0.305628[/TD]
[TD="align: right"]0.383502[/TD]
[TD="align: right"]1.000000[/TD]
[TD="align: right"]-0.706011[/TD]
[/TR]
[TR]
[TD="align: right"]-0.129392[/TD]
[TD="align: right"]-0.095889[/TD]
[TD="align: right"]-0.706011[/TD]
[TD="align: right"]1.000000[/TD]
[/TR]
</tbody>[/TABLE]

But when I replace the .Correl with PEARSONRHO, I get error:

[TABLE="width: 256"]
<colgroup><col span="4"></colgroup><tbody>[TR]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD]d[/TD]
[/TR]
[TR]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[/TR]
[TR]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[/TR]
[TR]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[/TR]
[TR]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[TD="align: center"]#VALUE![/TD]
[/TR]
</tbody>[/TABLE]

Why is this happening?
 
Upvote 0
The modified function, which is giving error, is:

Code:
Public Function PEARSONMAT(ByVal rng As Range) As Variant

    Dim outRslt() As Double, i As Long, j As Long, iCols As Long


    iCols = rng.Columns.Count
    ReDim outRslt(iCols - 1, iCols - 1)


    For i = 1 To iCols
        For j = 1 To iCols
            outRslt(i - 1, j - 1) = PEARSONRHO(rng.Columns(i), rng.Columns(j))
        Next j
    Next i


    PEARSONMAT = outRslt

End Function
 
Upvote 0
The PEARSONRHO function fails here.
Code:
    For k = 1 To x.Rows.Count
        sx = x(k) + sx
        sy = y(k) + sy
    Next
Not 100% sure why but code stops execution on this line.
Code:
sx = x(k) + sx

If I step through the code and stop at the line immediately before the above line and then enter this,
Code:
? x(k)
in the Immediate Window I get a type mismatch error.

What should x(k) return?
 
Upvote 0
When rng.Columns(i) is passed to PEARSONRHO, it receives a Range object. However, since it receives a column, you'll need to refer to the Cells property of the Range object. Also, since it refers to a worksheet range, the reference is a two dimensional one. So, for example, you should do it like this...

sx = x.cells(k,1).value + sx
 
Upvote 0
Try this.
Code:
Public Function PEARSONRHO(ByVal x As Variant, ByVal y As Variant) As Variant
Dim sx As Double, sy As Double
Dim s1 As Double, s2 As Double, s3 As Double
Dim k As Long

    For k = 1 To x.Rows.Count
        sx = x.Cells(k, 1).Value + sx
        sy = y.Cells(k, 1) + sy
    Next

    sx = sx / x.Rows.Count
    sy = sy / y.Rows.Count

    For k = 1 To x.Rows.Count
        s1 = s1 + (x.Cells(k, 1).Value - sx) * (y.Cells(k, 1).Value - sy)
        s2 = s2 + (x.Cells(k, 1).Value - sx) ^ 2
        s3 = s3 + (y.Cells(k, 1).Value - sy) ^ 2
    Next

    PEARSONRHO = s1 / Sqr(s2 * s3)

End Function

PS You could probable avoid having to use Cells if you passed arrays to PEARSONRHO rather than ranges.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top