+ Reply to Thread
Results 1 to 6 of 6

Covariance Matrix in excel

  1. #1
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Covariance Matrix in excel

    Hi! I am trying to complete a portfolio analysis in excel. We are doing this in a finance class and I am using VBA to make it easier and faster with calculations. I am left with a covariance matrix which needs to vary by size. I have tried to start where I calculate the first stock with the rest(vertical). But I am not sure why the code won`t work:

    Please Login or Register  to view this content.
    Expl:
    wb.hs.Cells(i + 1, 2) starts with row 2 and column 2(will turn into variable later).
    wb.ws.Range([Cells(1, 1), Cells(h, 1)]) is first stock
    wb.ws.Range([Cells(1, i), Cells(h, i)]) is first stock and increases by 1 stock range each time i increases.

    I appreciate any help. Thank you.
    Last edited by olli.excel; 10-27-2017 at 05:47 PM.

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Covariance Matrix in excel

    A Sheets collection is always associated with a particular workbook. If none is specified, the ActiveWorkbook.Sheets collection is assumed, so ws and hs were improperly defined. Note that the parent workbook is part of the worksheet object variable definition and need not be specified later. Just use ws and hs, not wb.hs.
    Please Login or Register  to view this content.
    If you wish to address sheets with the same name in different workbooks, define a string variable and use it as so:
    Please Login or Register  to view this content.
    Last edited by leelnich; 10-27-2017 at 10:38 PM.
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  3. #3
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Re: Covariance Matrix in excel

    Thank you for the answer! I changed and tried it again. Got error message; "run time error 1004, the item with the spesified name wasn`t found!" on the line:

    wb.hs.Cells(i, s) = wf.Covar(ws.Range([Cells(1, s-1), Cells(h, s-1)]), ws.Range([Cells(1, i-1), Cells(h, i-1)]))

    I just modified to more variables, but it is the same Idea.

    Thank you for the help. Can send more detailed in 1.5 hour if that is needed to modify.

  4. #4
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Covariance Matrix in excel

    Quote Originally Posted by olli.excel View Post
    run time error 1004 = "Application-defined or object-defined error" on the line:

    Please Login or Register  to view this content.
    Remove the wb. at the beginning. You're specifying workbook TWICE, it's already part of the hs definition.
    Also, I just realized the "internal" Cells references all apply to the ActiveSheet! You must specify ws for EACH ONE.

    Try this:
    Please Login or Register  to view this content.
    Or better yet:
    Please Login or Register  to view this content.
    BTW, forum rules ask for CODE tags around your code (so it looks like the code above).
    In Editing Mode, just select your code text and click the # button on the editing toolbar above the body of your post.
    Last edited by leelnich; 10-27-2017 at 04:18 PM.

  5. #5
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Re: Covariance Matrix in excel

    Thank you so much! Modified and it worked perfectly with all the variables

  6. #6
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Covariance Matrix in excel

    You're welcome, happy to help! - Lee

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to calculate the Covariance for my Excel File?
    By albert_teo77 in forum Excel General
    Replies: 1
    Last Post: 02-16-2014, 10:33 AM
  2. Covariance matrix from worksheet
    By uncina in forum Excel Programming / VBA / Macros
    Replies: 28
    Last Post: 01-22-2014, 12:17 PM
  3. Replies: 6
    Last Post: 09-30-2013, 10:14 AM
  4. Covariance Matrix
    By andrewc in forum Excel General
    Replies: 1
    Last Post: 09-14-2011, 09:53 AM
  5. COVARIANCE.S to build a covariance matrix
    By steve.lorimer in forum Excel General
    Replies: 6
    Last Post: 06-05-2011, 09:37 AM
  6. variance covariance matrix
    By ctallen23 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2011, 01:34 AM
  7. Covariance Matrix in Data Analysis tool
    By andrewc in forum Excel General
    Replies: 0
    Last Post: 12-29-2009, 10:30 AM
  8. [SOLVED] CORRELATION / COVARIANCE MATRIX
    By Walker in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-30-2005, 02:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1