Copying Pairs of Data Columns & Calculating their Correlation Coefficient

rwmill9716

Active Member
Joined
May 20, 2006
Messages
493
Office Version
  1. 2013
Platform
  1. Windows
I have the following macro:

Sub Partition()
Dim LastCol As Long, Col As Long, LastRow As Long
Dim rData As Range

Application.ScreenUpdating = False
With Sheets("Raw Data")
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rData = .Range("A1").Resize(LastRow, LastCol)
End With
With Sheets("Calculated Results")
For Col = 2 To LastCol
.Range("B6").Resize(LastRow).Value = rData.Columns(1).Value
.Range("C6").Resize(LastRow).Value = rData.Columns(Col).Value
.Range("B6").Resize(LastRow, 2).Sort Key1:=.Range("C7"), _
Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next Col
End With
Application.ScreenUpdating = True
End Sub

It copies the A column from tab "Raw Data" into tab "Calculated Results" below and runs through each of the other columns in "Raw Data" in turn writing their results in tab "Calculated Results". The macro ends with the 1st and last columns of "Raw Data" in tab "Calculated Results." I need to change the macro to do the following:

1. Instead of contrasting just "Raw Data" column 1 with all the other columns, I need it to cycle through all possible pairs, i.e., doing column 1 versus column 2 and above, then column 2 versus column 3 and above to column next-to-last versus column last (here columns 9 and 10, but these could run to 100 columns in tab "Raw Data"; Note, I refer here to data column labels 1-10 instead of column letters;

2. For each pair brought over to columns B and C of calculate a correlation coefficient (Correl(array 1, array 2)) and row titles in columns E through I row 7; each time a pair is brought over calculated these row 7 values then step down to copy the next set in row 8; for 10 columns of data, there will be 45 of these rows.

As it stands now with this macro, it copies the 1st column from "Raw Data" into column B of "Calculated Results", then cycles through the remaining columns of "Raw Data" to end up with the table below and only this pair's information in row 7.



Excel 2003
BCDEFGHI
6AMC in CHMDHMI in CHMDCorrelationPair
7110-0.927110AMC in CHMDHMI in CHMD
81343.134
9121.51521351.486
10110.35541390.642
11103.62671402.796
12103.51351403.865
13102.1891404.508
14101.51424.5
15100.73191439.393
Calculated Results
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,215,461
Messages
6,124,958
Members
449,200
Latest member
indiansth

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