I am pretty new to VBA hence my current struggle and clumsy code. Within my code I am trying identify uncorrelated stocks for a study project. The idea is that my macro generates a correlation matrix, bounded the Stock Names and then loops through line by line to return the most "uncorrelated" stock for the underlying sample. However, I do not currently know whether there is just a referencing issue in my code or whether there is any fundamental issue. Any help would be much appreciated
Find below the relevant part of the script:
Sub Test()
Dim Names As Variant
Dim Ref As Variant
Set Names = Worksheets("Correlation Matrix").Range(Cells(1, 3), Cells(1, Stocks))
For i = 1 To Stocks
Worksheets("Correlation Matrix").Activate
Ref = Worksheets("Correlation Matrix").Range(Cells(i + 1, 3), Cells(i + 1, Stocks)).Value
Worksheets("Main").Activate
Worksheets("Main").Cells("O" & i + 4).FormulaArray = "=Index(" & Names.Address & ",MATCH(MIN(ABS(" & Ref.Address & " - 0),ABS(" & Names.Address & "- 0),0))"
Next i
End Sub
The "Correlation Matrix" worksheet looks like this:
Disney Microsoft Apple sp500 Disney 1 0.764790855 0.737566223 0.832602399 Microsoft 0.764790855 1 0.754724823 0.827980429 Apple 0.737566223 0.754724823 1 0.90982066 sp500 0.832602399 0.827980429 0.90982066 1
Stocksvariable defined?