In this case, how should I correctly define the range?

Ms.Sigma

New Member
Joined
Mar 1, 2011
Messages
35
There are several columns in a worksheet. I want to calculate the correlation matrix of these columns and display the correlation matrix in a user form.

In user form, there are one refedit box, one list box, and one command button.
The refedit box is for data selection; list box is for displaying correlation matrix. When command button is clicked, the correlation matrix will be displayed.

Here is my macros:


Function CorrelationMatrix(rng As Variant) As Variant
' Returns correlation matrix of a range
Dim i As Integer, j As Integer, K As Integer, ncols As Integer, nrows As Integer
Dim r1vector() As Variant
Dim r2vector() As Variant
Dim Cmatrix() As Variant
ncols = rng.Columns.Count
ReDim Cmatrix(ncols, ncols)
nrows = rng.Rows.Count
ReDim r1vector(nrows)
ReDim r2vector(nrows)
For i = 1 To ncols
For K = 1 To nrows
r1vector(K) = rng(K, i)
Next K
Cmatrix(i, i) = 1
For j = i + 1 To ncols
For K = 1 To nrows
r2vector(K) = rng(K, j)
Next K

Cmatrix(i, j) = Application.WorksheetFunction.Correl(r1vector, r2vector)
Cmatrix(j, i) = Cmatrix(i, j)
Next j
Next i

CorrelationMatrix = Cmatrix

End Function

Private Sub cmdCorrelation_Click()
Dim series As Range
Dim correlation As Variant
Set series = Range(RefEdit1.Text)
correlation = CorrelationMatrix(series)

With ListBox1
.Clear
.Font.Size = 9
.List() = correlation
End With
End Sub

The problem is: when I click command button, the correlation matrix cannot be displayed in list box.I think I have mistake for definition of range, but I don't know how should I modify this mistake. Could any one give me some suggestions?

Thanks for all your help!!!
 
many many thanks!! I got hint from your macros!!;)

One way:
Code:
Private Sub cmdCorrelation_Click()
    Dim avdCorr      As Variant
    Dim asCorr()    As String
    Dim i           As Long
    Dim j           As Long
 
    avdCorr = CorrelationMatrix(Range("A2:D9"))
    ReDim asCorr(1 To UBound(avdCorr, 1), 1 To UBound(avdCorr, 2))
    
    For i = 1 To UBound(avdCorr, 1)
        For j = 1 To UBound(avdCorr, 2)
            asCorr(i, j) = Format(avdCorr(i, j), "+0.000;-0.000;"" ""0")
        Next j
    Next i
 
    With Sheet1.ListBox1
        .ColumnCount = UBound(asCorr)
        .List() = asCorr
    End With
End Sub
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,224,586
Messages
6,179,713
Members
452,939
Latest member
WCrawford

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