# calculating covariance matrix from array in vba

#### centauri

##### New Member
I am trying to calculate a covariance matrix in VBA. I have managed to get my data into an array "datarng" which has 10 columns and 100 rows. I want to calculate the covariance matrix of my dataset so that I will have a new array 10x10 by calculating the covariance between each column in the original array. can anyone help?

### Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

#### pgc01

##### MrExcel MVP
Hi centauri

You mean the autocovariance matrix?

So, for ex., if your values are in A1:J100, the element (2,5) in the autocovariance matrix would be

=COVAR(B1:B100,E1:E100)

Is this what you want to do in vba?

#### centauri

##### New Member
Yes, this is what I'm trying to do, but I have an array of data that also varies in size so I need to set up a new array which will be a square matrix of size equal to the number of columns in the first array. How do I define the size of the array and the elements in the new array?

#### pgc01

##### MrExcel MVP
Hi

In this example I used an array 100x10 that I filled with random data.
I added some writing to the worksheet just so that you can easily check the result.

Select an empty worksheet and try:

Code:
``````Sub Test()
Dim dArrData(1 To 100, 1 To 10) As Double
Dim dAutoCoVar() As Double
Dim j As Long, k As Long

' fill the array with some bogus data
Randomize
For j = 1 To 100
For k = 1 To 10
dArrData(j, k) = Rnd
Next k
Next j

' write to the worksheet, for debug
Range("A1:J100").Value = dArrData

' calculate the autocovariance matrix
dAutoCoVar = Autocovar(dArrData)

' write to the worksheet, for debug
Range("L1:U10").Value = dAutoCoVar
End Sub

Function Autocovar(dArrData() As Double) As Double()
Dim dArrResult() As Double
Dim j As Long, k As Long

' redim the result array as a square array.
ReDim dArrResult(1 To UBound(dArrData, 2), 1 To UBound(dArrData, 2))

' calculate the autocovariance matrix
For j = 1 To UBound(dArrData, 2)
For k = 1 To UBound(dArrData, 2)
With Application.WorksheetFunction
dArrResult(j, k) = .Covar(.Index(dArrData, 0, j), .Index(dArrData, 0, k))
End With
Next k
Next j
Autocovar = dArrResult
End Function``````

Replies
3
Views
176
Replies
2
Views
72
Replies
4
Views
127
Replies
1
Views
127
Replies
3
Views
701

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,163,837
Messages
5,833,918
Members
430,244
Latest member
Ireland1

### 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.

### Which adblocker are you using?

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

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