Jul 11 2023 06:29 PM
As per your request , I state my PC specs. I have desk top HP . installed windows 10 education version 22H2 , OS Build 19045.3086. installed MICROSOFT 365 application for enterprise.
I am re-building Excel model to produce for me the efficient frontier line and weights for 5 stocks selected ( 1st model I created was for 20 stocks) . The model uses a matrix to calculate the Covariance among the 5 stocks .
I am using MACRO to run my model whenever the stocks selected changed or the input data changed.
My problem:
In my new 5 stocks model, I made a separate small Macro “MTX” to calculate the Covariance for the 5 stocks [ Sheet2, cell range E23 : i82] to test it before I copy/paste it in my large MACRO which is called “AutoSolver”.
This “MTX” Macro should work to fill specific matrix areas in Sheet # 2.
The MTX macro is NOT RECORDING THE STEPS I USED “Data, Data analysis , Covariance” at all ….. !!!! I do not know why , SEEMS THERE IS A BUG in MS 365 version. As before this version (excel 365) , when I created this model for 20 stocks [some time ago] under perhaps Excel 2016, Covariance matrix calculation worked fine and still working fine. But now, I can not use Covariance application under Data analysis icon in the new macro “MTX” I am creating.
To overcome this obstacle, I tried to create a very small separate Macro “Analy” using the Covariance application only without any other steps and copy paste it in the MTX macro . It worked one time , then stopped in 2nd test. the “MTX” Macro again stopped and says Error at this step shown below ( line 19 in MTX macro) :
Application.Run "ATPVBAEN.XLAM!Mcovar", ActiveSheet.Range("$E$23:$I$82"), _
ActiveSheet.Range("$D$157"), "C", False
Is there a solution to this problem?
n.b. The large main Macro “autosolver” is not yet amended in this file pending finding a solution to the matrix calculation. it is still working as if 20 stocks NOT 5 stocks .
I need to attach the Excel file but i do not know how ?
Jul 17 2023 05:56 AM
I understand that you are encountering issues with recording the steps for the "Data Analysis" tool in Excel 365 and experiencing errors in your macro. It is possible that there might be compatibility or configuration issues causing the problem. Here are a few suggestions to help resolve the issue:
Vba code:
Sub CalculateCovarianceMatrix()
Dim rngData As Range
Dim rngOutput As Range
' Specify the input data range
Set rngData = ThisWorkbook.Sheets("Sheet2").Range("E23:I82")
' Specify the output range for the covariance matrix
Set rngOutput = ThisWorkbook.Sheets("Sheet2").Range("D157")
' Calculate the covariance matrix using the COVAR function
rngOutput.FormulaArray = "=COVAR($E$23:$I$82)"
End Sub
Regarding attaching the Excel file, unfortunately, file attachments are not supported in this text-based interface. However, you can upload the file to a file-sharing service (e.g., Google Drive, Dropbox) and share the download link here so that I can take a closer look at your macro code if needed. Since no one has answered it, I entered your question in various AI’s. The text and the steps are the result of various AI's put together.
My answers are voluntary and without guarantee!
Hope this will help you.